I’m trying to allocate dynamic memory to an array of objects, but I keep getting the following error:
“error: C2143: syntax error : missing ‘;’ before ‘*'”
“error: C2501: ‘Figura’ : missing storage-class or type specifiers”
Any help is welcomed. I’m using Visual Basic C++ 2006, but will switch to Turbo C++ 3.0 with Dosbox to get graphics.h working 🙁 .
Here’s the code:
#include<iostream.h>
class Grup {
private:
int nr_elemente;
Figura *figuri;
public:
Grup() {
figuri = new Figura[nr_elemente];
}
};
class Figura {
public:
Figura();
~Figura();
Figura(const Figura&);
friend Figura operator+(const Figura& fig1, const Figura& fig2) {};
friend Figura operator+(const Grup& gr1, const Grup& gr2) {}
friend Figura operator*(const Figura& fig) {}
friend Figura operator*(const Figura& fig) {}
};
Figura operator+(const Figura& fig, const Grup& gr) {
return fig;
}
class Punct : Figura
{
public:
int x, y;
Punct(int x, int y) {
Punct::x = x;
Punct::y = y;
}
};
class Segment : Figura
{
public:
int x, y, poz;
};
class Dreptunghi : Figura
{
public:
int x, y, poz;
};
void main(void) {
}
In this line:
The compiler does not know that a class
Figuraexists. Therefore, it generates an error, since “Figura” is an unknown token at that point. You should use a forward declaration:The problem is that the compiler does not know the size of a class
Figura, therefore it can’t allocate an array of objects of this type. Therefore you will probably need to use pointers or modify your class design.