【问题标题】:C++ Input Operator OverloadingC++ 输入运算符重载
【发布时间】:2012-12-30 18:22:54
【问题描述】:

我正在尝试在我创建的 UserLogin 类上重载输入运算符。不会引发编译时错误,但也不会设置值。

一切都在运行,但 ul 的内容仍然存在: 字符串 id 是 sally 登录时间为 00:00 注销时间为 00:00

入口点

#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;
}

【问题讨论】:

  • 你确定这是代码吗? friend istream 运算符采用 const 引用,但读入对象不能是 const 操作。
  • @juanchopanza 是的,但是输入运算符中使用的强制转换意味着 OP 不是读入对象,而是读入临时对象(可能在 VC 上,以便临时对象可以绑定到内置 @987654325 @ 非常量引用)。

标签: c++ class operator-overloading inputstream


【解决方案1】:

您对operator&gt;&gt; 的定义是错误的。您需要通过非常量引用传递 user 参数,并摆脱强制转换:

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

    return input;
}

强制转换导致您读入临时文件,然后立即丢弃。

【讨论】:

  • 很有趣,因为我在 Time 课上做了同样的事情,而且效果很好。但是,您提到的更改已经解决了问题...
  • @user1960364 那么接受答案怎么样? (这就是 SO 通常的工作方式)。
【解决方案2】:
input >> (type) var;

错了,不要这样做。做简单的事

input >> var;

【讨论】:

  • 我添加了演员表,因为它抛出了一个错误。演员表解决了错误,但显然不是 User 是 const 的根本问题。
  • 添加演员表是一种对抗您不理解的错误的方法,但从长远来看,剪掉手指会更有效且痛苦更少。 澄清我不主张采取任何一种行动。
【解决方案3】:
#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

【讨论】:

    猜你喜欢
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    相关资源
    最近更新 更多