-1

I'm trying to output the values of a vector array into a text but im getting this weird error

no opeartor "<<" matches the operands, operand types are : std::ofstream << Person

Here is my code

class Person {
public:
    Person(string, string, int);
    string get_name() {
        return name;
    }
    string get_family() {
        return family;
    }
    int get_age() {
        return age;
    }



private:
    string name;
    string family;
    int age;


};

    ofstream myfile;
    myfile.open("test.txt");

    vector <Person> persons;


    string N, F;
    int A;
    while (cin >> N >> F >> A) {

        Person tmpPerson(N, F, A);

        persons.push_back(tmpPerson);

        for (int i = 0; i < persons.size(); i++){
            myfile << persons[i] << " " << endl;
        }
        myfile.close();

    };

I have incuded the and tried everything, but this error, persist, would be very thankful for some help !

FreeSock
  • 51
  • 7
  • Not related to the stated problem however you need to get your for loop and `myfile.close()`. out of your while loop. – drescherjm Mar 23 '18 at 18:51
  • 1
    Did you provide a `operator <<` overload that takes a `std::ostream` reference on the left side and a `Person` on the right? If not, there's your problem. If yes, you didn't do it correctly. If the latter, we can't possibly help because you didn't include it in your posted code. – WhozCraig Mar 23 '18 at 18:52
  • Please provide [mcve]. For instance: Does the class `Person` contain overloaded `operator<<`? – Algirdas Preidžius Mar 23 '18 at 18:52
  • @AlgirdasPreidžius The class can't contain that overload. It needs to be a free function (possibly friended to the mystery `Person` class). If the OP tried to implement it as a member function it wouldn't work (and maybe that's the problem, but without code, we'll never know, as you've pointed out). – WhozCraig Mar 23 '18 at 18:54
  • @WhozCraig OK, I agree that it was poor choice of words. What I meant was: "Is `operator<<` overloaded for class `Person`?" – Algirdas Preidžius Mar 23 '18 at 19:01
  • @WhozCraig Added the Person class – FreeSock Mar 23 '18 at 19:04

1 Answers1

0

You need to add a function helper like this for your custom class Person.

ostream& operator<<(ostream& os, const Person& p)  
{  
    os << p._N << ' '  << p._F << ' ' << p._A << std::end; 

    return os;  
}
Ratah
  • 299
  • 3
  • 11