0

I want to be able to go through and print out each Profile in an array of Profiles. Even just trying to print a single item from the array, I get a million errors I cannot even decipher. Everything else works just fine, its only the last line that messes up. How do I print a class specific function from an array of class objects? How do I make it loop?



#include <iostream>
#include <vector>
using namespace std;

class Profile {
  public:
    //set things up
    std::string name;
    int age;
    std::string city;
    std::string gender;
    std::vector<std::string> hobbies;
    std::vector<std::string> preferences;
    std::vector<std::string> pronouns;
  
  public:
    //make it so Profile must have name, age, city, and your own gender
    Profile(std::string new_name, int new_age, std::string new_city, std::string new_gender);
    //print out information about the person
    void view_bio() {
      std::cout << name << " is a " << age << " year old " << gender << ". \n"; 
      if(pronouns.size() > 0) {
        std::cout << "The pronouns " << name << " uses are: \n";
        for (std::string noun : pronouns) {
          std::cout << noun << "\n";
        }
      }
      if(hobbies.size() > 0) {
      std::cout << "Their hobbies include:\n";
      for (std::string hobby : hobbies) {
        std::cout << hobby << "\n";
      }
      }
    }
    //add to items that have multiple things
    void add_hobby(std::string new_hobby) {
      hobbies.push_back(new_hobby);
    }
    void add_preference(std::string new_preference) {
      preferences.push_back(new_preference);
    }
    void add_pronouns(std::string new_pronouns) {
      pronouns.push_back(new_pronouns);
    }
    //creepy dating
    int max_age() {
      int max = (age-7)*2;
      return max;
    }
    int min_age() {
      int min = age/2+7;
      return min;
    }
};

//make it so Profile creation must have these things
Profile::Profile(std::string new_name, int new_age, std::string new_city, std::string new_gender){
      name = new_name;
      age = new_age;
      city=new_city;
      gender=new_gender;
    };

//Compare if two Profiles are compatable at face value
bool compare(Profile date1, Profile date2) {
    if (!(date1.age > date2.min_age() && date1.age < date2.max_age() && date2.age > date1.min_age() && date2.age < date1.max_age() && date1.city == date2.city)){
      return false;
    }
    if (date1.preferences.size() == 0 && date2.preferences.size() == 0){
      return true;
      }
    else if (date1.preferences.size() > 0 && date2.preferences.size() == 0){
      for (std::string pref : date1.preferences){
        if(pref == date2.gender){
          return true;
        }}
        return false;
        }
    else if (date2.preferences.size() > 0 && date1.preferences.size() == 0){
      for (std::string pref : date2.preferences){
        if(pref == date1.gender){
          return true;
        }}
        return false;
        }
    else {
      for (std::string pref : date1.preferences){
        if(pref == date2.gender){
          for (std::string ferp : date2.preferences){
            if(ferp == date1.gender){
              return true;
              }}}}
        return false;
      }
      return false;
    };

int main() {
Profile mags("Mags", 21, "Cincinati", "Woman");
Profile hen("Hen", 23, "Cincinati", "Man");
std::cout << compare(mags, hen);
Profile online[2] = {mags, hen};
std::cout << online[1];
}
2
  • 1
    you need to create an operator << for your class. see here for example learn.microsoft.com/en-us/cpp/standard-library/… Commented Dec 8, 2022 at 1:59
  • 1
    Can you explain what, exactly, do you expect std::cout << online[1]; to do, and how, specifically, you expect this to happen? Where does the shown code define the << overload for your class? Nothing in C++ happens automatically. << knows how to format basic data types, and a few classes like std::string. That's it. For everything else, and especially your own classes, you must define your own overloaded operator. Commented Dec 8, 2022 at 2:01

1 Answer 1

1

use something like this

ostream& operator <<(std::ostream& stream, const Profile& profile) {
  stream << profile.city << std::endl;
  stream << profile.gender << std::endl;
  // etcetera
  return stream;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.