2

I'm trying to print every element inside a vector like this:

vector<users>::iterator i;

for(i = userlist.begin(); i<userlist.end(); i++)
{
        cout << *i << "\n";
}

Then I'm getting an error like this:

no match for 'operator<<' in 'std::cout << (&i)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = users*, _Container = std::vector<users, std::allocator<users> >]()' 

Is it anything obvious I've missed?

user709712
  • 253
  • 1
  • 6
  • 13

5 Answers5

6

Have you defined a function with this signature?:

std::ostream & operator<<(std::ostream &, const users &);

It should not be a member function of users, although it may or may not be a friend, up to you. The prototype should go in the header file of class users, and the body should go in the source(.cpp) file. I have no idea how your users class is defined, or how you would want to format the output, but the function definition should look something like this:

std::ostream & operator<<(std::ostream & os, const users & U)
{
    os << U.some_data_members;
    os << U.and_or_some_member_functions();
    os << whatever;
    return os;
}
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • No, not sure where to define it. In the header of the user class? – user709712 Apr 18 '11 at 19:44
  • @user: Put the prototype in the header, exactly as above. But put the body in the source(.cpp) file. – Benjamin Lindley Apr 18 '11 at 19:45
  • Okey, I've now defined the prototype in the header, but not exactly sure how to build the function in the source file. Also, I'm getting this error after I defined the prototype in the header: `std::ostream& user::operator<<(std::ostream&, const user&)' must take exactly one argument` Sorry, I'm new to C++. – user709712 Apr 18 '11 at 20:00
2

Once you've defined std::ostream &operator<<(std::ostream &, user&);, consider changing your code to use std::copy instead of a for loop:

// leaving off the `std::`, you're not using it for `cout`.
// 
copy(userlist.begin(), userlist.end(), ostream_iterator<user>(cout, "\n"));
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

Have you defined stream operator for the users class? If not, do so.

levis501
  • 4,117
  • 23
  • 25
1

You need to write an overload of ostream::operator<<() that takes a an instance of users, or write some conversion operator that will provide an auto-conversion from user to some type that one of the operator<<() versions knows about.

QuantumMechanic
  • 13,795
  • 4
  • 45
  • 66
1

You need to define your own public function operator<< taking parameters of an ostream and a users:

std::ostream& operator<<(std::ostream&, users&);

Sorry, is it users or user?

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • It's `users`, but I guess I'll change it, looks better with `user`. But where do I define the function? In the header of the user class? And do I have to create a function inside the user class aswell? – user709712 Apr 18 '11 at 19:40
  • You usually declare the function in the header just below the user class, and define it in the module file alongside the user member functions. – quamrana Apr 18 '11 at 19:51