helpdesk
helpdesk

Reputation: 2074

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:

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

and here is the .cpp declaration:

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

the class has one private variable called 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&)

Upvotes: 0

Views: 163

Answers (2)

Jonathan Wakely
Jonathan Wakely

Reputation: 171393

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

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

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.

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.

So do:

#include <istream>

at the top of the op.cpp file.

Presumably you'd included <iosfwd> somewhere, directly or indirectly, which declares std::istream as a type, but not included <istream> which defines it fully.

Upvotes: 7

Joseph Mansfield
Joseph Mansfield

Reputation: 110728

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.

Upvotes: 3

Related Questions