Reputation: 3
I want to overload the operator >> so that when I use cin>> I will be allowed to set my object's attributes,the overloading of << is working but not >>, here is my code.`
#include <iostream>
#include <cstdlib>
using namespace std;
class fraction{
private:
int den;
int nom;
public:
fraction(){nom=0;den=1;}
fraction(int x,int y){nom=x;den=y;}
fraction(const fraction &);
~fraction();
friend ostream& operator<<(ostream &,const fraction &);
friend istream& operator>>(istream &,fraction &);
fraction operator*(const fraction &);
fraction operator/(const fraction &);
fraction operator+(const fraction &);
fraction operator-(const fraction &);
friend void simple(fraction &);
};
istream operator>>(istream& is,fraction &v){
is>>v.nom;
is>>v.den;
return is;
}
` And here is the error:
main.cpp: In function ‘std::istream operator>>(std::istream&, fraction&)’:
main.cpp:10:7: error: ‘int fraction::nom’ is private
main.cpp:53:4: error: within this context
main.cpp:9:7: error: ‘int fraction::den’ is private
main.cpp:54:4: error: within this context
In file included from /usr/include/c++/4.7/ios:43:0,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from main.cpp:1:
/usr/include/c++/4.7/bits/ios_base.h: In copy constructor ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’:
/usr/include/c++/4.7/bits/ios_base.h:788:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
In file included from /usr/include/c++/4.7/ios:45:0,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from main.cpp:1:
/usr/include/c++/4.7/bits/basic_ios.h:64:11: error: within this context
In file included from /usr/include/c++/4.7/iostream:41:0,
from main.cpp:1:
/usr/include/c++/4.7/istream: In copy constructor ‘std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)’:
/usr/include/c++/4.7/istream:56:11: note: synthesized method ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ first required here
main.cpp: In function ‘std::istream operator>>(std::istream&, fraction&)’:
main.cpp:55:9: note: synthesized method ‘std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)’ first required here
Can anyone tell me what's wrong ?
Upvotes: 0
Views: 122
Reputation: 2518
Declare the function header for the overloaded operator as follows:
istream &operator>>(istream& is,fraction &v){
The & was missing for the return value.
Upvotes: 2