BebliucGeorge
BebliucGeorge

Reputation: 751

Array of objects dynamic memory allocation

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) {



}

Upvotes: 0

Views: 922

Answers (3)

clanmjc
clanmjc

Reputation: 299

You need to forward declare, already mentioned, but that is not enough, you need to know the size of the class in order to use it as an implementation. Therefore...

class Figura;
class Grup{
  Figura * figuri;
  //.. other stuff, but ONLY declarations no implementations.
};

class Figura
{
  //Same as you already have...
};

After the classes have been declared, since they are in the same file you need to implement AFTER the other classes have been implemented (or declared).

//Still same file but below the Figura class definition..
Grup::Grup() {
   //Now you can use Figura...
   figuri = new Figura[nr_elemente];
}  

@be The problem you are seeing with your comment below has to do with the assignment operator for your fig class.

The operator looks like this... Fig& operator=(const Fig& );

But with your example you are assigning a pointer void func(const Fig * fig) { //... figcontainer[index] = fig; // if you did *fig; you would be ok //...
}

Upvotes: 0

Artak Begnazaryan
Artak Begnazaryan

Reputation: 469

Your problem is that you're trying to do all in one file. You'll not have such kind of problems if you move the declaration and definition of classes to separate .hpp and .cpp files. Move

// file grup.hpp

class Figura; // forward declaration

class Grup {
private:
    int nr_elemente;
    Figura *figuri;
public:
    Grup();
};

to grup.hpp file. Then implement the constructor of Grup in group.cpp file:

// file grup.cpp
#include "grup.hpp"
#include "figura.hpp"


Grup::Grup()
{
    figuri = new Figura[nr_elemente];
}

Also move the definition and declaration of Figura class to corresponding figura.hpp and figura.cpp files. Better to use the same approach with all classes you have.

Upvotes: 0

mfontanini
mfontanini

Reputation: 21900

In this line:

figuri = new Figura[nr_elemente];

The compiler does not know that a class Figura exists. Therefore, it generates an error, since "Figura" is an unknown token at that point. You should use a forward declaration:

class Figura; // forward-declare class Figura

class Grup {
/* ... */
};


class Figura {
/* ... */
};

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.

Upvotes: 2

Related Questions