(C ++)重载“ >>”运算符的问题

时间:2018-11-07 20:09:00

标签: c++

一旦我尝试编译并运行程序,视觉就会显示此错误。

  

错误1错误C2679:二进制'>>':未找到采用'const char [2]'类型的右侧操作数的运算符(或没有可接受的转换)

过载功能:

istream& operator>> (istream& InputStream, Description& rhs) {
InputStream >> rhs.mNumber >> "," >> rhs.mLenght >> "," >> rhs.mName;

return InputStream;
}

类描述的定义:

class Description {
private:
    int mNumber;
    int mLenght;
    string mName;
public:
    Description();
    Description(int, int, string);
    Description& operator= (const Description&);
    friend ostream& operator<< (ostream&, Description&);
    friend istream& operator>> (istream&, Description&);
};

是的,我做到了:

#include <iostream>
#include <string>
#include <fstream>
#include <istream>

1 个答案:

答案 0 :(得分:1)

在线

InputStream >> rhs.mNumber >> "," >> rhs.mLenght >> "," >> rhs.mName;

","部分错误。您无法将任何内容读入字符串文字中。

如果您希望在输入流中看到令牌,,则可以使用:

char dummy;
InputStream >> rhs.mNumber >> dummy >> rhs.mLenght >> dummy >> rhs.mName;