Overloading >> Operator

Sorry, accidentally edited the post.

But I am having trouble overloading the stream extraction operator.
Each time I try, I get the following error

Can any of you spot my error? I am new to the concept of overloading operators so I am sure it is probably a 'newbie' mistake.


Description Resource Path Location Type
Symbol 'lName' could not be resolved overload.cpp /overload line 42 Semantic Error
Symbol 'fName' could not be resolved overload.cpp /overload line 40 Semantic Error
Symbol 'lName' could not be resolved overload.cpp /overload line 40 Semantic Error
Symbol 'fName' could not be resolved overload.cpp /overload line 42 Semantic Error



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
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <fstream>

using namespace std;

class testClass
{
public:
	// code

	string fName, lName;

	friend istream& operator >> (istream&, testClass&);

	void printName();

private:
	// code
protected:
	// code
};
int main()
{
	ifstream inData;
	inData.open("name.txt");

	testClass object;

	inData >> object;
	object.printName();

	return 0;
}

istream& operator >> (istream& inData, testClass& object)
{
	// code

	cout << "Gathering Data..." << endl;
	inData >> fName >> lName;

	cout << "Name: " << fName << " " << lName;
	cout << endl << endl;

	return inData;
}

void testClass::printName()
{
	cout << "First Name: " << fName << endl;
	cout << "Last Name: " << lName;
}


TEXT FILE: Peter Griffin
Last edited on
What is the problem you are having? Is the program compiling? If not, what are the errors?
I just updated the post and the code to include the error report
You've defined the operator as being a friend function outside the class, and yet you're trying to use fName and lName in the operator as if it were a class member.
Last edited on
Mikey, If I am not mistaking... aren't friend classes able to access all members of classes?
Mikey, If I am not mistaking... aren't friend classes able to access all members of classes?

Yes they are but as you say these are members of an object.
write object.fName and object.lName instead of fname and lname in the operator.
Last edited on
Aaah, I see. that indeed fixed the Compilation error. However the program still does not output values as expected. Here is updated code

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
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>

using namespace std;

class testClass
{
public:
	// code

	string fName, lName;

	friend istream& operator >> (istream&, testClass&);

	void printName();

private:
	// code
protected:
	// code
};
int main()
{
	ifstream inData;
	inData.open("name.txt");

	testClass object;

	inData >> object;
	object.printName();

	return 0;
}

istream& operator >> (istream& inData, testClass& object)
{
	// code

	//cout << "Gathering Data..." << endl;
	inData >> object.fName >> object.lName;

	return inData;
}

void testClass::printName()
{
	cout << "First Name: " << fName << endl;
	cout << "Last Name: " << lName;
}
Works fine here:
http://cpp.sh/3j52

What's the content of the file and what is the output?
Last edited on
The content of the file is a first name and a last name ... say "Peter Griffin"
Yeah, it works here without any problems.
Topic archived. No new replies allowed.