5

I'm trying to overload the input operator on a UserLogin class I've created. No compile-time errors are thrown, however the values are not set either.

Everything runs, but it the content of ul remains: string id is sally Time login is 00:00 Time logout is 00:00

Entry Point

#include <iostream>
#include "UserLogin.h"

using namespace std;

int main() {
    UserLogin ul;

    cout << ul << endl; // xxx 00:00 00:00
    cin >> ul; // sally 23:56 00:02
    cout << ul << endl; // Should show sally 23:56 00:02
                        // Instead, it shows xxx 00:00 00:00 again

    cout << endl;
    system("PAUSE");
}

UserLogin.h

#include <iostream>
#include <string>
#include "Time.h"

using namespace std;

class UserLogin
{
    // Operator Overloaders
    friend ostream &operator <<(ostream &output, const UserLogin user);
    friend istream &operator >>(istream &input, const UserLogin &user);

    private:
        // Private Data Members
        Time login, logout;
        string id;

    public:
        // Public Method Prototypes
        UserLogin() : id("xxx") {};
        UserLogin( string id, Time login, Time logout ) : id(id), login(login), logout(logout) {};
};

UserLogin.cpp

#include "UserLogin.h"

ostream &operator <<( ostream &output, const UserLogin user )
{
    output << setfill(' ');
    output << setw(15) << left << user.id << user.login << " " << user.logout;

    return output;
}

istream &operator >>( istream &input, const UserLogin &user )
{
    input >> ( string ) user.id;
    input >> ( Time ) user.login;
    input >> ( Time ) user.logout;

    return input;
}
2
  • Are you sure that is the code? The friend istream operator takes a const reference, but reading into the object couldn't be a const operation. Commented Jan 16, 2013 at 12:22
  • 1
    @juanchopanza Yes, but the casts used in the input operator mean the OP is not reading into the object, but into temporaries (probably on VC, so that the temporaries can bind to built-in >> non-const references). Commented Jan 16, 2013 at 12:24

3 Answers 3

13

Your definition of operator>> is wrong. You need to pass the user argument by non-const reference, and get rid of the casts:

istream &operator >>( istream &input, UserLogin &user )
{
    input >> user.id;
    input >> user.login;
    input >> user.logout;

    return input;
}

The casts are causing you to read into a temporary, which is then immediately discarded.

Sign up to request clarification or add additional context in comments.

2 Comments

Interesting, because I did the same thing in my Time class and it worked perfect. However, the changes you mentioned have solved the problem...
@user1960364 How about accepting the answer, then? (That's how SO generally works).
0
input >> (type) var;

is wrong, don't do this. Do simple

input >> var;

2 Comments

I added the casts because it was throwing an error. The casts solved the error but apparently not the underlying problem of User being const.
Adding casts is a way to fight errors you don't understand, but cutting your fingers away is more effective and less pain in the long run. clarification I am not advocating either action.
-2
#ifndef STRING_H
#define STRING_H

#include <iostream>
using namespace std;

class String{
    friend ostream &operator<<(ostream&,const String & );


    friend istream &operator>>(istream&,String & );

public:
    String(const char[] = "0");
    void set(const char[]);
    const char * get() const;
    int length();

    /*void bubbleSort(char,int);
    int binSearch(char,char,int);

    bool operator==(const String&);
    const String &operator=(const String &);
    int &operator+(String );*/

private:
        const char *myPtr;
    int length1;

};
#endif

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.