difficulty with definging def outside of class

Hi,
In the code below I am trying to extract one of the definitions from within a class to be defined outside of the class and am having a hard time doing so:

original 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
50
51
52
53
54
55
#include <iostream>

class Vector3d
{
private:
	double m_x, m_y, m_z;

public:
	Vector3d(double x = 0.0, double y = 0.0, double z = 0.0)
		: m_x(x), m_y(y), m_z(z)
	{

	}

	void print()
	{
		std::cout << "Vector(" << m_x << " , " << m_y << " , " << m_z << ")\n";
	}
};

class Point3d
{
private:
	double m_x, m_y, m_z;

public:
	Point3d(double x = 0.0, double y = 0.0, double z = 0.0)
		: m_x(x), m_y(y), m_z(z)
	{

	}

	void print()
	{
		std::cout << "Point(" << m_x << " , " << m_y << " , " << m_z << ")\n";
	}

	void moveByVector(Vector3d &v)
	{
		// implement this function as a friend of class Vector3d
	}
};

int main()
{
	Point3d p(1.0, 2.0, 3.0);
	Vector3d v(2.0, 2.0, -3.0);

	p.print();
	p.moveByVector(v);
	p.print();

	return 0;
}


I need to : Make Point3d a friend class of Vector3d, and implement function Point3d::moveByVector(). I've had many tries and typically get errors saying it cant find my def in the class prototypes that sort of thing:

in class proto:
 
void moveByVector(Vector3d &v);

out of class def:
 
void Point3d::moveByVector(Vector3d &v)

1
2
3
{
	//content here
}
Last edited on
Are you after something like this: https://ideone.com/pMnXpB ?

Made Point3d a friend, move definition out of class. FWIW, I think making friends here is overkill - would make more sense to me to expose whatever you need from the vector on some public interface.
Topic archived. No new replies allowed.