error when trying to print vector



When I try to compile my code, I get an error about there not being a match for cout 'operator<<'. As I'm new to C++, I don't know what the problem is. In other programs, I've been able to print the contents of vectors using a loop like this. What exactly am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  #include<fstream>
#include<iostream>
#include<vector>
using namespace std;

class Course
{public:
  long courseNum;
  string courseName;
};

class Student
{public:
  long stNo;
  Course Course1;
  Course Course2;
  Course Course3;
};

class Person
{public:

  long SSN;
  string fName;
  string lName;
  int age;
  char gender;
};

class StudentProfile
{public:

  Person PersonalInfo;
  Student StdInfo;

  void print(const vector<StudentProfile>& StuCollection)
  {

    for(int j=0; j < StuCollection.size(); j++)
      {
        cout << StuCollection[j] << endl; // the error has something to do with this portion
      }
  }

  void getInfo()
  {

  }
};

int main()
{
  vector<StudentProfile> StuCollection;

  StudentProfile new_stu;

  ifstream fin;
  fin.open("Information.txt");
  int i = 0;
  while(fin)
    {
      fin >> new_stu.PersonalInfo.fName >> new_stu.PersonalInfo.lName >> new_stu.PersonalInfo.SSN >> new_stu.PersonalInfo.age >> new_stu.PersonalInfo.gender >> new_stu\
.StdInfo.stNo >> new_stu.StdInfo.Course1.courseName >> new_stu.StdInfo.Course1.courseNum >> new_stu.StdInfo.Course2.courseName >> new_stu.StdInfo.Course2.courseNum >> \
new_stu.StdInfo.Course3.courseName >> new_stu.StdInfo.Course3.courseNum;

      StuCollection.push_back(StudentProfile());
      i++;
    }

  fin.close();
}
The problem is that you have a vector of StudentProfiles, but std::cout has no idea how to print out that structure. You'll need to tell it how to do it somehow, probably by defining your own operator << for it.
There is a question of the design too. Why would an individual StudentProfile have a print function which deals with a vector of StudentProfiles? It's as if one individual student is expected to handle the data for all of the other students.
I haven't been taught how to define operators. I'm currently researching how to do that, but I'm having a hard time understanding. As far as the design goes, that's the template my lab instructor gave us. You're right, it doesn't quite make sense, so I've pulled that out of the class.
> I haven't been taught how to define operators.

Just write separate (ideally non-member) print() functions for now. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void print( const Course& c )
{ std::cout << "course{" << c.courseNum << ',' << c.courseName << '}' ; }

void print( const Student& s )
{
    std::cout << '#' << s.stNo << ' ' ;
    print( s.Course1 ) ;
    std::cout << ' ' ;
    print( s.Course2 ) ;
    std::cout << ' ' ;
    print( s.Course3 ) ;
}

void print( const Person& p )
{
    std::cout << "SSN: " << p.SSN << ' ' << p.fName << ' ' << p.lName
               << ' ' << p.age << " years " << p.gender ;
}

void print( const StudentProfile& sp )
{
    print( sp.PersonalInfo ) ;
    std::cout << '\n' ;
    print( sp.StdInfo ) ;
    std::cout << '\n' ;
}

void print( const vector<StudentProfile>& StuCollection )
{
    for( std::size_t j = 0 ; j < StuCollection.size(); ++j )
    {
        print( StuCollection[j] ) ;
        std::cout << '\n' ;
    }
}
I haven't been taught how to define operators. I'm currently researching how to do that, but I'm having a hard time understanding.


They basically look like this

1
2
3
4
5
6
7
8
9
10
std::ostream & operator<<( std::ostream &stm , const Object &obj ) 
{
    return( stm << obj.stuff );
}

int main()
{
    Object obj;
    std::cout << obj << std::endl;
}
I appreciate the help, but it isn't making any sense to me. I'll just keep trying to figure it out.
Just a lil addition to giblit's comment, to use his overloaded operator<< you have to make it a friend of your class, otherwise the class would not give any access to its private data member(unless of course your data members are public).
My classes are public for this project. I've tried using the syntax provided above, but I'm still getting compile errors. I've spent over four hours trying to figure this out. Any specific examples related to my code?
Topic archived. No new replies allowed.