简体   繁体   English

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

[英]I/O operator overloading error in C++

please, perhaps, I am too tired cuz I can't seem to figure out why this input stream operator overloaded function is complaining..here is the .h declaration: 请,也许吧,我太累了,因为我似乎无法弄清楚为什么这个输入流运算符重载的函数会抱怨..这是.h声明:

friend std::istream& operator>>(std::istream& in,  IntOperatorClass& intoperatorClass)

and here is the .cpp declaration: 这是.cpp声明:

std::istream& operator >>(std::istream& in,  IntOperatorClass& intoperatorClass)
     {
         in>>intoperatorClass.value;
         return in;
     }

the class has one private variable called value.. 该类具有一个称为value的私有变量。

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

is there something I am doing wrong because the compiler keeps complaining that the ">>" is not defined.. 我在做错什么吗,因为编译器一直抱怨“ >>”没有定义。

./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&)

Let's break the error down: 让我们分解错误:

../op.cpp:77: error: no match for 'operator>>' in
'in >> intoperatorClass->IntOperatorClass::value'

Firstly, the compiler is telling you the error is in op.cpp on line 77 首先,编译器在第77行告诉您错误在op.cpp

I bet you that's this line: 我敢打赌你就是这样:

     in>>intoperatorClass.value;

The next bit of the error is: 错误的下一部分是:

no match for 'operator>>' in 'in >> intoperatorClass->IntOperatorClass::value'

The compiler is saying that inside your operator>> it doesn't know how to use another operator>> to extract from an istream into your value member, which is an int 编译器说,在您的operator>>它不知道如何使用另一个operator>>istream提取到您的value成员(即int

It then tells you that the only candidate it has that looks like an operator>> is your one, which extracts into an IntOperatorClass not an int . 然后,它告诉您它唯一看起来像operator>>候选operator>>是您的候选operator>> ,它将而不是int提取到IntOperatorClass

It should have a whole lot of other candidates, for extracting into integers and doubles and strings and other types. 它应该有很多其他候选对象,用于提取为整数,双精度数和字符串以及其他类型。 The only way the compiler wouldn't know how to do that is if you haven't included the header that defines istream and its operators. 编译器不知道如何执行此操作的唯一方法是,如果未包含定义istream及其操作符的标头。

So do: 这样:

#include <istream>

at the top of the op.cpp file. op.cpp文件的顶部。

Presumably you'd included <iosfwd> somewhere, directly or indirectly, which declares std::istream as a type, but not included <istream> which defines it fully. 大概您在直接或间接地将<iosfwd>包含在某个位置,该位置将std::istream声明为一种类型,但没有包括<istream>对其进行了完全定义。

Don't forget to include the appropriate header: 不要忘记包含适当的标题:

#include <istream>

Presumably you include some header that is including std::istream in some way but not pulling in the full contents of the <istream> header. 大概您包含一些标头,该标头以某种方式包含std::istream ,但没有提取<istream>标头的全部内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM