Need help with Pointers

Hi all,
I really need help.
I have base class Shape2D and subclasses Square, Rectangle and Cross.
I want to store user input x and y coordinates (int) data in Shape and access by subclasses to compute area which is a virtual function. I'm really confused with pointers.
Below is my code for Shape2D and Square. Assn2 is where I have my main() function.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class Assn2
{
	private:

		int nPos;
		ShapeTwoD * pShape2d[4];	//create 4 array of pointers


	public:
		//Function prototypes

		Assn2();
		void displayMainMenu();		
		void inputSensorData();
		int computeArea4All();
		void printShapesReport();
		void sortShapeData();
		void sortAreaAscending();
};

Assn2::Assn2()
{
	nPos=0;			//initialise array position
}

int main ()
{
	//nPos = 0;
	Assn2 assignment2;
	ShapeTwoD shape2D;
	Square aSquare;
	ShapeTwoD *Square = &aSquare;
}

void Assn2::inputSensorData()
{
	if(shape == "Square" || shape == "square")
	{
	pShape2d[nPos] = new Square();
	for (int i=0; i<4; i++)
	{
		cout << "Please enter x-ordinate of pt." << i+1 << " : ";
		cin >> x ;
		pShape2d[nPos].setX(x);
		cout << "Please enter y-ordinate of pt." << i+1 << " : ";
		cin >> y;
		pShape2d[nPos].setY(y);
	}
}

class ShapeTwoD
{
	protected: 
		string name;
		bool containsWarpSpace;
		double area;
	
	public:
		int x, y;
		ShapeTwoD();
		ShapeTwoD (string, bool);
		string getName();
		bool getContainsWarpSpace();
		string toString();
		//virtual function override by sub-classes and implemented individually
		virtual double computeArea();		
		void setY (int);
		void setX (int);
};

void ShapeTwoD::setX (int newX)
{
	x = newX;
}

void ShapeTwoD::setY (int newY)
{
	y = newY;
}



Error:

Assn2.cpp: In member function ‘void Assn2::inputSensorData()’:
Assn2.cpp:146:18: error: request for member ‘setX’ in ‘((Assn2*)this)->Assn2::pShape2d[((Assn2*)this)->Assn2::nPos]’, which is of non-class type ‘ShapeTwoD*’
Assn2.cpp:149:18: error: request for member ‘setY’ in ‘((Assn2*)this)->Assn2::pShape2d[((Assn2*)this)->Assn2::nPos]’, which is of non-class type ‘ShapeTwoD*
Last edited on
The errors are because you are using the dot operator . when you should be using the -> operator

kbear wrote:
Assn2 is where I have my main() function.
What do you mean by this?
Lines 44 and 47 of your posted code (which evidently line up with lines 146 and 149 of your real code) call the setX and setY functions on the object pointed to by pShape2d, which is of type ShapeTwoD*. However, Shape2D is not declared until later in the file. When compiling lines 44 and 47, the compiler has no idea what ShapeTwoD is or that it contains member functions setX and setY.

(Additionally, based on the code you posted, the compiler has no idea what a Square is or that it is derived from a ShapeTwoD.)

I don't know if your code is all in one file or if you broke your code into appropriate header and source files for each of your classes. If it is all in one file (as implied by your posted code), ShapeTwoD must be declared (i.e. lines 51 - 69) before the code can create one (line 30) or call a member function on one (line 44 or 47).
LB, Thanks. i have changed to -> operator. :) I meant to say Assn2.cpp has the main() function. Sorry to have confused you. I'm still trying to familiarise with the terms and all that C++ stuff.

doug4: My base class ShapeTwoD and derived classes have individual header and source files.

And another thing is....

I have 3 derived classes: Cross, Rectangle and Square. Each have different x and y values input by user. Eg, Cross has 12 sets of x,y coordinates, Rectangle and Square have 4 sets each. Can I know how to store this array of data x and y in ShapeTwoD by using pointers?

For this sets of code below, how do I write such that 4 sets of x,y coordinates will be stored in ShapeTwoD?

1
2
3
4
5
6
7
8
9
10
11
12
13
	if(shape == "Square" || shape == "square")
	{
	pShape2d[nPos] = new Square();
	for (int i=0; i<4; i++)
	{
		cout << "Please enter x-ordinate of pt." << i+1 << " : ";
		cin >> x ;
		pShape2d[nPos]->setX(x);
		cout << "Please enter y-ordinate of pt." << i+1 << " : ";
		cin >> y;
		pShape2d[nPos]->setY(y);
	}
	}


I will also need to pass the x and y values to derived classes to compute area.

1
2
3
4
5
6
7
8
9
10
11
12
13
//Square.cpp
//this is virtual function

double Square::computeArea()
{
	for (int i=0; i<4; i++)
	{
	double length = sqrt(pow((x[i] - x[i+1]), 2) + pow((y[i] - y[i+1]), 2));
    	double area = pow(length, 2);
	cout << "Square Area: " << area << endl;
	break;
	}
}


This is what I'm trying to achieve. When computeArea4All() is called, computeArea() function in derived classed will be implemented.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Assn2.cpp

int Assn2::computeArea4All()
{
	int nCount=0;
	for (int i = 0; i<nPos; i++) 
	{	
		double area = pShape2d[i]->computeArea();
		pShape2d[i]->setArea(area);
		nCount++;
	}
	cout << "\nComputation completed! ( " << nCount << " records were updated)" << endl;
	return nCount;
}


I hope I'm making sense here. Thanks so much for helping. Have been stuck with this for days and still can't figure out.
Last edited on
For this sets of code below, how do I write such that 4 sets of x,y coordinates will be stored in ShapeTwoD?


You can't. What you can do is put 4 sets of coordinates in the Square class. Or maybe store the edge length and rotation angle in the Square class and calculate the corners based on the center point (from ShapeTwoD) and the rotation and size.

ShapeTwoD is the base class, so it only contains members that are common to all its derived classes. A square has 3 corners, but a circle doesn't have any. They both have a location, but the interpretation of the location may be different for different shapes. I personally would probably let the location be the center of all my derived shapes, but it could be a specific corner or center of an important arc if you want to define it as such.

The derived classes add the members that they need to specialize them from the base class. So, a square could have coordinates for it corners (if that's how you want to represent a square). A circle would probably just have a radius.

You might want to have Square derive from rectangle. However you end up representing these classes, the underlying Rectangle information can be reused for Square.
@doug4 : Thanks. I used arrays to store x and y coordinates in derived classes and created pointer arrays of object to invoke virtual function for polymorphism. It's working now.
Topic archived. No new replies allowed.