Stumped With Objects

Feb 25, 2010 at 11:51pm
The entire programming exercise is supposed to find the distance between two points in 3D space. Every file that is included is necessary per instructor guidelines. I have fixed everything down to 5 seemingly simple errors:


error C2504: 'MyPoint' : base class undefined
error C2065: 'x' : undeclared identifier
error C2065: 'y' : undeclared identifier
error C2065: 'x' : undeclared identifier
error C2065: 'y' : undeclared identifier

However, that base class is defined, blatantly. Here is all of the source code:

Main Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cmath>
#include "MyPoint.h"
#include "ThreeDPoint.h"
using namespace std;

double getDistance(ThreeDPoint &points1, 
		           ThreeDPoint &points2)
{
	return sqrt(pow((points1.getX() - points2.getX()), 2.0) +
		        pow((points1.getY() - points2.getY()), 2.0) +
				pow((points1.getZ() - points2.getZ()), 2.0));
}

int main()
{
	ThreeDPoint set1(0.0, 0.0, 0.0);
	ThreeDPoint set2(10.0, 30.0, 25.5);

	cout << getDistance(set1, set2);
	return 0;
}


MyPoint Implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "MyPoint.h"

MyPoint::MyPoint()
{
	x = 0.0;
	y = 0.0;
}

MyPoint::MyPoint(double newX, double newY)
{
	x = newX;
	y = newY;
}

double MyPoint::getX()
{
	return x;
}

double MyPoint::getY()
{
	return y;
}


MyPoint Class Definition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef MYPOINT_H
#define MYPOINT_H

class MyPoint
{
private:
	double x, y;

public:
	friend class ThreeDPoint;
	
	MyPoint();
	MyPoint(double, double);

	double getX();
	double getY();
};

#endif 


ThreeDPoint Implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "ThreeDPoint.h"

ThreeDPoint::ThreeDPoint()
{
	x = 0.0;
	y = 0.0;
	z = 0.0;
}

ThreeDPoint::ThreeDPoint(double newX, double newY, double newZ)
{
	x = newX;
	y = newY;
	z = newZ;
}

double ThreeDPoint::getZ()
{
	return z;
}


ThreeDPoint Class Declaration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef THREEDPOINT_H
#define THREEDPOINT_H

class ThreeDPoint:   public MyPoint
{
private:
	double z;

public:
	ThreeDPoint();
	ThreeDPoint(double, double, double);
	
	double getZ();
	//double getDistance(); 
};

#endif 


Feb 26, 2010 at 12:42am
Did you include the MyPoint header and what not in your 3D point material?
Feb 26, 2010 at 12:45am
Also, you don't have to make ThreeDPoint a friend of MyPoint, just make MyPoint's private variables protected.
Feb 26, 2010 at 12:47am
Yup. In case you don't know (because protected is that funny third label), protected means that if I had an object of derived and an object of base, and the base had protected stuff, the derived objects could access the protected stuff. But the general user would still not be able to get at it.
Feb 26, 2010 at 3:13am
*cough cough* http://cplusplus.com/forum/articles/10627/#msg49679
*cough cough* Section 4

MyPoint is an "include dependency" of ThreeDPoint, therefore threedpoint.h should be #include'ing "mypoint.h"

+ the protected thing
Feb 26, 2010 at 3:14am
THANK YOU THANK YOU THANK YOU

I cannot believe that I missed that #include statement. I typed that in, removed the friend class statement, and put the necessary variables in protected status, and IT WORKS!
Topic archived. No new replies allowed.