no match for operator << ?? why ??
I see no reason why the compiler is complaining in the implementation of
operator<< starting at line 22
this might be a dumb error, forgive me
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
|
class User {
public :
User(
const char* username,
const char* password )
: username_( username ), password_( password ) { }
void changeUserName( const char* username ) { username_ = username; }
void changePassword( const char* password ) { password_ = password; }
const char* username() const { return username_.c_str(); }
const char* password() const { return password_.c_str(); }
inline friend std::ostream& operator<< ( const std::ostream& stream,
const User& user );
inline friend std::istream& operator<< ( const std::istream& stream,
const User& user );
private :
std::string username_;
std::string password_;
};
std::ostream& operator<< ( const std::ostream& stream, const User& user )
{
return( stream << user.username_ << user.password_ );
}
std::istream& operator>> ( const std::istream& stream, const User& user )
{
return( stream >> user.username << user.password_ );
}
// ...
|
Last edited on
See something weird here?:
1 2 3 4
|
inline friend std::ostream& operator<< ( const std::ostream& stream,
const User& user );
inline friend std::istream& operator<< ( const std::istream& stream,
const User& user );
|
:)
1 2 3
|
inline friend std::istream& operator<< ( const std::istream& stream, // wrong <<
const User& user );
|
1 2 3 4
|
std::istream& operator>> ( const std::istream& stream, const User& user )
{
return( stream >> user.username << user.password_ ); // wrong <<
}
|
well that was really dumb, unfortunately it produces again the same error :
???
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
|
class User {
public :
User(
const char* username,
const char* password )
: username_( username ), password_( password ) { }
void changeUserName( const char* username ) { username_ = username; }
void changePassword( const char* password ) { password_ = password; }
const char* username() const { return username_.c_str(); }
const char* password() const { return password_.c_str(); }
inline friend std::ostream& operator << ( const std::ostream& stream,
const User& user );
inline friend std::istream& operator >> ( const std::istream& stream,
const User& user );
private :
std::string username_;
std::string password_;
};
std::ostream& operator<< ( const std::ostream& stream, const User& user )
{
return( stream << user.username_ << user.password_ );
}
std::istream& operator>> ( const std::istream& stream, const User& user )
{
return( stream >> user.username >> user.password_ );
}
|
Streams are changed by both input and output, so you need to pass them by non-const reference.
Also for the input operation, the user object must be passed by non-const reference as well.
23 24 25 26 27 28 29 30 31 32
|
std::ostream& operator<< ( std::ostream& stream, const User& user )
{
return( stream << user.username_ << user.password_ );
}
std::istream& operator>> ( std::istream& stream, User& user )
{
return( stream >> user.username >> user.password_ );
}
|
oh, i see thanks for the help !!!
Topic archived. No new replies allowed.