insertion operator

Whats wrong with this code i tried but still it won't work why won't it pick the insertion function i made

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 #include<iostream>
#include<conio.h>
#include<string>
#include<iomanip>
using namespace std;
class Gradebook
{
private:
	string code;
	string landline;
	string area;
public:
	friend ostream & operator<<(Gradebook &, ostream &);
	friend istream & operator>>(Gradebook &, istream &);
};
ostream & operator<<(Gradebook & phone, ostream & output)
{
	output<<"("<<phone.code<<")"<<" "<<phone.landline<<"-"<<phone.area<<endl;
	return output;
}
istream & operator>>(Gradebook & phone, istream & input)
{
	input.ignore();
	input>>setw(3)>>phone.code;
	input.ignore(2);
	input>>setw(3)>>phone.landline;
	input.ignore();
	input>>setw(4)>>phone.area;
	return input;
}
int main()
{
	Gradebook P;
	cout<<"Enter number in the formate of (000) 123-5831\n";
	cin>>P;
	cout<<"Your number is :";
	cout<<P;
}
I believe that the input and output insertion operators have to be defined like this:
1
2
std::ostream& operator<< (std::ostream& output, const Gradebook& phone);
std::istream& operator>> (std::istream& input, Gradebook& phone);


Note that the input / output comes first, it should always be the left hand argument.
Last edited on
Sorry but still it doesn't work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class Gradebook
{
private:
	string code;
	string landline;
	string area;
public:
	friend ostream & operator<<(ostream &, const Gradebook &);
	friend istream & operator>>(istream &, Gradebook &);
};
ostream & operator<<(ostream & output, const Gradebook & phone)
{
	output << "(" << phone.code << ")" << " " << phone.landline << "-" << phone.area << endl;
	return output;
}
istream & operator>>(istream & input, Gradebook & phone)
{
	input >> setw(3) >> phone.code;
	input.ignore(2);
	input >> setw(3) >> phone.landline;
	input.ignore();
	input >> setw(4) >> phone.area;
	input.ignore();

	return input;
}
int main()
{
	Gradebook P;
	cout << "Enter number in the formate of (000) 123-5831\n";
	cin >> P;
	cout << "Your number is :";
	cout << P;

	cin.ignore();
	return 0;
}
Last edited on
Sharan123 wrote:
Sorry but still it doesn't work

Did you change both the friend declaration and the definition?
Yes Thank you it works now , i get it we had to write ostream/istream in function argument , function call first memeber is insertion operator and is overloaded first and than object is called
Topic archived. No new replies allowed.