C++ C+中的I/O运算符重载错误+;
请注意,也许我太累了,因为我似乎不明白为什么这个输入流操作符重载函数在抱怨..下面是.h声明:C++ C+中的I/O运算符重载错误+;,c++,C++,请注意,也许我太累了,因为我似乎不明白为什么这个输入流操作符重载函数在抱怨..下面是.h声明: friend std::istream& operator>>(std::istream& in, IntOperatorClass& intoperatorClass) std::istream& operator >>(std::istream& in, IntOperatorClass& intoperatorClas
friend std::istream& operator>>(std::istream& in, IntOperatorClass& intoperatorClass)
std::istream& operator >>(std::istream& in, IntOperatorClass& intoperatorClass)
{
in>>intoperatorClass.value;
return in;
}
以下是.cpp声明:
friend std::istream& operator>>(std::istream& in, IntOperatorClass& intoperatorClass)
std::istream& operator >>(std::istream& in, IntOperatorClass& intoperatorClass)
{
in>>intoperatorClass.value;
return in;
}
该类有一个名为value.的私有变量
班级:
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;}
};
不要忘记包含适当的标题:
#include <istream>
#包括
假设您包含了一些标题,这些标题以某种方式包含了
std::istream
,但没有包含
标题的全部内容。让我们来分析一下错误:
。/op.cpp:77:错误:在
'在>>InOperatorClass->intoperatorClass::value'
首先,编译器告诉您错误在第77行的op.cpp
中
我敢打赌这是一条线:
in>>intoperatorClass.value;
错误的下一位是:
no match for 'operator>>' in 'in >> intoperatorClass->IntOperatorClass::value'
编译器说,在您的运算符>>
中,它不知道如何使用另一个运算符>>
将istream
提取到您的值
成员中,该成员是int
然后它会告诉您,它拥有的唯一一个看起来像操作符>
的候选者是您的候选者,它将提取到一个到操作符类中,而不是一个int
它应该有很多其他候选项,用于提取整数、双精度、字符串和其他类型。编译器不知道如何做到的唯一方法是,如果您没有包含定义istream
及其操作符的头
我们也要这样做:
#include <istream>
#包括
在op.cpp
文件的顶部
想必您已经直接或间接地在某个地方包含了
,它将std::istream
声明为一个类型,但没有包含定义它的
。什么类型的值
?编译器说重载中的输入值不匹配。itz是int类型。。我仍然不明白计算机的意思。它到底是什么类型的?@henryjoseph,显示你的类定义。看起来你需要在.cpp文件中包含。在C++03中
不能保证定义istream
,在C++03和C++11中它都添加了(一点点)初始化标准流不必要的开销,因此
比hanks更好。我本来会接受你的答案,但在你之前有人接受了,但我把你投了赞成票。再次感谢