拜托,也许我太累了,因为我似乎无法弄清楚为什么这个输入流运算符重载函数在提示......这是.h声明:
friend std::istream& operator>>(std::istream& in, IntOperatorClass& intoperatorClass)
这是 .cpp 声明:
std::istream& operator >>(std::istream& in, IntOperatorClass& intoperatorClass)
{
in>>intoperatorClass.value;
return in;
}
该类有一个名为 value 的私有(private)变量..
类(class):
class IntOperatorClass{
int value; //a value
int arr[10];//an array value
public:
IntOperatorClass();//default constructor
IntOperatorClass(int);//constructor with parameter
IntOperatorClass(int arr[], int length);//an array constructor
IntOperatorClass(const IntOperatorClass& intoperatorClass);//copy constructor
IntOperatorClass& operator=(const IntOperatorClass& intoperatorClass);//assignment operator
friend std::ostream& operator <<(std::ostream& out, const IntOperatorClass& intoperatorClass);//output operator;
friend std::istream& operator>>(std::istream& in, IntOperatorClass& intoperatorClass);//input operator
IntOperatorClass& operator++();//pre increment operator
IntOperatorClass operator++(int);//post increment operator
friend IntOperatorClass operator+(const IntOperatorClass intoperatorClassFirst , const IntOperatorClass intoperatorClassSecond);
int GetValue(){return value;}
};
是不是我做错了什么,因为编译器一直提示“>>”没有定义..
./op.cpp: In function 'std::istream& operator>>(std::istream&, IntOperatorClass&)':
../op.cpp:77: error: no match for 'operator>>' in 'in >> intoperatorClass->IntOperatorClass::value'
../op.cpp:75: note: candidates are: std::istream& operator>>(std::istream&, IntOperatorClass&)
最佳答案
让我们分解错误:
../op.cpp:77: error: no match for 'operator>>' in<br/>
'in >> intoperatorClass->IntOperatorClass::value'
首先,编译器告诉您错误在 op.cpp
中在第 77 行
我敢打赌,就是这一行:
in>>intoperatorClass.value;
错误的下一位是:
no match for 'operator>>' in 'in >> intoperatorClass->IntOperatorClass::value'
编译器说在你的 operator>>
里面它不知道如何使用另一个 operator>>
从 istream
中提取进入你的value
成员,这是一个 int
然后它会告诉您它拥有的唯一候选对象看起来像 operator>>
是你的,提取到 IntOperatorClass
不是int
.
它应该有很多其他候选对象,用于提取整数、 double 、字符串和其他类型。编译器不知道如何做到这一点的唯一方法是,如果您没有包含定义 istream
的 header 。及其运营商。
也一样:
#include <istream>
在 op.cpp
的顶部文件。
大概你已经包括了<iosfwd>
在某个地方,直接或间接地声明 std::istream
作为一种类型,但不包括在内<istream>
完整地定义了它。
关于c++ - C++ 中的 I/O 运算符重载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15305340/