#include <iostream>
#include <fstream>
usingnamespace 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
}
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;
}
}
}
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.
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.
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.