2d pointer???

Hi everyone

I am working on this problem of classes shape.

I am reading different shapes from a data file.(square(s 10 10 1 1). circle etc)

I am making a pointer array [50] in main()

then I am supposed to add the shapes from the file to this array using a function addShapes

int addShapes(pointer** ptr, int maxArray)
where it returns the total objects from file.

I am having a hard time coming up with a solution on how to connect the function with the array in main

Please help me with some ideas or hints to point me in the right direction.

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
#include <iostream> 
#include <fstream>

using namespace std;

// adds object from the data file
// int is the number for the array
int addSapes( CShape**, int); 
int main()
{
	CShape* [50];
	ifstream in;

	in.open("in.txt");

	// if not open display errror

}
int addShapes( CShape** ptr, int maxArray)
{	
	int n // number of objects created

	// not quite sure how to connect this with CShape* [50] in Main()???
	
	return n // total number of objects created
}


Thanks for the time
Last edited on
1
2
3
4
5
int main(){
   Charpe* myshapes[50];
   addShapes(myshapes, 50);
   ...
}

1
2
3
4
5
void addShapes(CShape** ptr, int maxArray){
   for (int i = 0; i < maxArray; i++)
      ptr[i] = new CShpe();//or something
   ...
}
(not tested)
Thanks for the reply

I have played a litle with the code and I am making it worse I think

can you take a look and see where am I wrong

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

int addShapes( Shape**, int); 
int main()
{
	Shape* p[50];
	
	addShapes(p, 50);
	//in.close();
	return 0;

}
int addShapes( Shape ptr[][], int maxArray)
{	
	int n = 0; // number of objects created
	ifstream in;
	char ch;

	int x, y, r, f=0, c=0;

	// not quite sure how to connect this with CShape* [50] in Main()???
	in.open("in.txt");
	if (!in.is_open())
		cout << "File did not open." << endl;

	ptr = new Shape* [50];// not sure what this is doing

	while (!in.eof())
	{

		in >> ch;
		switch (ch)
		{
		case 'c':// circle (c 40 9 4 1 1);

			in >> x >> y >> r >> c >> f;

			cout << ch << x << y << r << fs << cl << endl; 

			//delete [] ptr[50];
			//delete [] ptr;


		}
	}
}


Last edited on
I would love to help you but I am a bit confused.

You are not including any header for CShape and it is not defined in that code. Also how does CShape* [50] compile? There is no identifier for it so it should not be legal.

CShape* shapes[50]; is an array of 50 pointers. I take it that all of the CShape objects have set methods that you call after reading in a shape from "in.txt" right?

What exactly so you mean by connecting the shape? It looks like you are trying to pass in a pointer to a pointer.
I have declared othere classes using inheritance where my base class is Shape.

and circle , triangle, dimond, rectungle are derived from shape.

now I have a txt file that have different shapes with arguments.

I should read the file and store the right shape at the right class and then I can use the print which is virtual in a loop

but I do not know how to read the file and add them to my: Shape* ptr[50]

using the function: int addShape(Shape** ptr, int maxArray

I hope I am making some sense
Well I am going to bed now but I would probably write a function that takes a stream, reads in a shape from the stream, creates a new shape with it, and then returns a pointer to it.

If you can do that than you can just loop this until eof and store it into your array each iteration. There is no need to be using double pointers in your case.

*Also always make sure a pointer is not NULL before you dereference it.
1
2
3
4
5
6
int* ptr;

if (ptr)
    // ok, dereference it
else
   // do not dereference, will cause a seg fault! 



And since you are using polymorphism I assume you understand that only virtual methods will work on derived classes through the base class pointer. If you want to call a non-virtual method in a derived class you will need to do a dynamic cast.

Have fun.
Last edited on
ptr = new Shape* [50] overwrites the previous value of ptr so that it doesn't have any relation to the array in main. You shouldn't be doing this. The memory has already been allocated.

I can't say why ->draw() causes a crash. It shouldn't happen, unless new failed, which is very unlikely.. Could something inside Circle::draw() cause a crash?

Notice that you're working with ptr[0] only. If you have several shapes in the file, only the last one will be read. It should be ptr[i] where i starts from 0 and is incremented on every cycle (until it reaches maxArray).

Well I am going to bed now but I would probably write a function that takes a stream, reads in a shape from the stream, creates a new shape with it, and then returns a pointer to it.
That just means wrapping lines 32-46 in a function. It hardly simplifies anything.
Topic archived. No new replies allowed.