Hi Guys,

I am having issues with reading from a list made up of lists. I dont want to paste my code as it is for an assignment, but here is the basic setup.

i have a class person that has declared

list<char> name;

push to the list;

i have a class School that has declared

list<person> people;

push person to people from the other list;

So now the List name People should contain a single row of names.

when i try to print from the list People i get the following CRAZY error that i have no idea about. I can print from List name without a problem.

This is the code used for printing :

void printList()
{
   list<person>::const_iterator itr;
   
   for (itr = people.begin(); itr != people.end(); itr++ ) {
      cout << *itr;
   }
}

The error message is as follows in g++ compiler

maze5.cpp: In member function 'void School::printList()':
maze5.cpp:78: error: no match for 'operator<<' in 'std::cout << itr. std::_List_const_iterator<_Tp>::operator* [with _Tp = person]()'
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:67: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:78: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:90: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:125: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:159: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:102: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/ostream:176: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/ostream:187: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/ostream:191: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/ostream:202: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:183: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:218: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:242: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/ostream:217: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:265: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:288: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:311: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/ostream:447: note:                 std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:509: note:                 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char) [with _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/ostream:458: note:                 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char) [with _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/ostream:463: note:                 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char) [with _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:571: note:                 std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:616: note:                 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*) [with _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/ostream:497: note:                 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*) [with _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/ostream:502: note:                 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*) [with _Traits = std::char_traits<char>]

Edited 6 Years Ago by doolsta111: n/a

It is probably because you don't have a operator<< function declared for your person class. Since it's a class, the compiler won't know how to 'cout' whatever data you want.

I believe this should be something along the lines of what you'd need

ostream& operator<<(ostream& outs, person& x) {
outs << "Name: " << person.name();
return (outs);
}

Edited 6 Years Ago by sblass92: typo

It is probably because you don't have a operator<< function declared for your person class. Since it's a class, the compiler won't know how to 'cout' whatever data you want.

I believe this should be something along the lines of what you'd need

ostream& operator<<(ostream& outs, person& x) {
outs << "Name: " << person.name();
return (outs);
}

Thanks for that. Yes the problem was what you had said there it doesnt know what to print in class. So i iterated within the List School and asked it to run the printfunction in Person. This does what i want.


BUT NOW, the problem im having is that my object of class person is not getting cleared, and the data i cout is coming like this.

person1 , person 1 + person 2, person1, + person2 + person3

Vs
person1, person2, person3.

The reason is i havent cleared the object of Class person before it adds to the list in School Class.

I tried

person Teacher = new person();

(gets error)

but this did not work in the While loop,
and i tried

person Teacher; and this also did not work. (doesnt make any changes)

Anyone have idea on how to clear the object of the class.

class person{
private: 
list<char> name;
}

If I understand correctly, this is what you've declared in person class and when you assign the name you use name.push() for lists. If its a list, name.clear() is a function provided that will empty your list.

Might I suggest replacing list<char> name; with:

string name;
//This way instead of using name.push(), you can treat name as a //variable and use the assignment operator. 

Teacher.name = "ABCD";
Teacher.name = "EFGH";

The old string will be written over and name will hold "EFGH".

person Teacher = new person();

The reason this did not work is because the new operator will return a pointer to your object in the memory heap.

person* Teacher = new person();

is a valid statement. From what you've posted, the difference between using pointers and copying the new person to your list is a matter of preference. For larger classes, its more efficient to store the pointer in the list rather than copy all the data.

This article has been dead for over six months. Start a new discussion instead.