c++ - C++ 中的 I/O 运算符重载错误

标签 c++

拜托,也许我太累了,因为我似乎无法弄清楚为什么这个输入流运算符重载函数在提示......这是.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/

相关文章:

c++ - 如何删除 vector 中的重复字符串/数据?

c++ - 浮点值加倍和除法

c++ - 使用 Visual Studio 调试时看不到 boost::optional 内容

c++ - 不用g++编译c代码的主要原因是什么?

c++ - JMS uri 问题

c++ - 在 C++ 中实现 sql 语句绑定(bind)的最佳方法

c++ - 使用基于动态/状态的分配器的 STL 实现?

c++ - 从右手边切 Qstring

python - 如何捕获包含在 boost python 模块中的 C++ 代码中抛出的 Python 异常

C++ 构造函数 : Perfect forwarding and overload