My overloaded std::istream function is a friend of the class, but it still can't access private members; why?

As the title says, I'm having this really dumb problem when trying to overload the input operator>> with class objects when the overloaded function has already been friended. The output operator<< is perfectly fine, though; for some reason, it's only giving me an error about the input operator>> overloaded function.

The class code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef POINT_H

#include <iostream>

class Point
{
private:
	double m_dX = 0.0, m_dY = 0.0, m_dZ = 0.0;
public:
	Point();
	Point(const double dX, const double dY, const double dZ);
	double GetX() const;
	double GetY() const;
	double GetZ() const;
	friend std::ostream& operator<< (std::ostream &out, const Point &cPoint);
	friend std::istream& operator>> (std::istream &in, const Point &cPoint);
};

#endif 


and
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
#include "Point.h"

Point::Point()
{
	m_dX = 0.0;
	m_dY = 0.0;
	m_dZ = 0.0;
}

Point::Point(const double dX, const double dY, const double dZ)
	:m_dX{ dX }, m_dY{ dY }, m_dZ{ dZ }
{
}

double Point::GetX() const
{
	return m_dX;
}

double Point::GetY() const
{
	return m_dY;
}

double Point::GetZ() const
{
	return m_dZ;
}


Then here's the file with main():
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
#include <iostream>
#include "Point.h"
#include "keep_window_open.h"

int main()
{
	using namespace std;
	cout << "Enter a point:\n";

	Point cPoint;
	cin >> cPoint;

	cout << "You entered " << cPoint << "\n";

	keep_window_open();
	return 0;
}

std::ostream& operator<<(std::ostream &out, const Point &cPoint)
{
	out << "(" << cPoint.m_dX << ", " <<
		cPoint.m_dY << ", " <<
		cPoint.m_dZ << ")";
	return out;
}

std::istream& operator>> (std::istream &in, Point &cPoint)
{
	in >> cPoint.m_dX;
	in >> cPoint.m_dY;
	in >> cPoint.m_dZ;
	return in;
}


For anyone who's curious, here are keep_window_open.h and its .cpp file:
1
2
3
4
5
6
#ifndef KEEP_WINDOW_OPEN_H
#define KEEP_WINDOW_OPEN_H

void keep_window_open();

#endif 


1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

void keep_window_open()
{
    using namespace std;
    cin.clear();
    cout << "Please enter a character to exit\n";
    char ch;
    cin >> ch;
	cin.ignore();
}


So what do I do? Is there something I've done wrong that I'm not noticing?

Edit: And doing it with the public access functions keeps producing the error that operator>> and an object of type double incompatible (which I know they shouldn't be).

Edit2: After overloading with a double-type object, I got this error:
1>ClassPoint.obj : error LNK2019: unresolved external symbol "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class Point const &)" (??5@YAAEAV?$basic_istream@DU?$char_traits@D@std@@@std@@AEAV01@AEBVPoint@@@Z) referenced in function main
1>c:\users\osman\programming\visual studio 2015\Projects\ClassPoint\x64\Debug\ClassPoint.exe : fatal error LNK1120: 1 unresolved externals
Last edited on
Let's put the declaration and first line of the definition in proximity:

1
2
friend std::istream& operator>> (std::istream &in, const Point &cPoint);
       std::istream& operator>> (std::istream &in,       Point &cPoint)


See any inconsistencies?
Last edited on
It's a simple error: on line 16 of "point.h" you declared the cPoint argument as a const reference. Remove the const. (You want to modify cPoint.)

Hope this helps.
Yeah, I got it. Taking out the const keyword helped. Problem solved. Thanks.
I'm having this really dumb problem when trying to overload the input operator>> with class objects when the overloaded function has already been friended.


The problem is that you haven't created the implementation that matches your definition in the class.

In the class definition you have:
friend std::istream& operator>> (std::istream &in, const Point &cPoint);

In main you have:
std::istream& operator>> (std::istream &in, Point &cPoint)
Notice the difference in the second parameter.

By the way posting your compiler error messages would have made this problem easier to solve.


Topic archived. No new replies allowed.