Edeph
Edeph

Reputation: 772

Class within class - incomplete type is not allowed

class Publicatie{
public:
    class Carte : public Publicatie{
    private:
        char* autor;
    };
    class Revista : public Publicatie{
    private:
        char* frecventa_aparitie;
        int numar;
    };
private:
    int cota;
    char* titlu;
    char* editura;
    int anul_aparitiei;
    int tiraj;
    Carte* c;
    Revista* r;
        public:
           //some methods...
}

This is the code, i'm declaring the class Carte and Revista inside the class Publicatie and i need to have private members Carte and Publicatie. I really don't know how to do the design with inheritance with these classes. I get the error in the title for the inheritance :public Publicatie and i thought that it will work because the class is already created ( even though it's private members were not created yet).

Upvotes: 0

Views: 973

Answers (2)

sazary
sazary

Reputation: 966

your design is wrong. you're trying to define a class, and in it's definition you're trying to use from itself; it is a logical paradox.
from what i can understand from your code, you're trying to create a class named Publicatie that represents a publication (or a post) and it has two other variants, named Carte and Revista. if this is the case, why the Publicatie needs to have two private members of type Carte and Revista? maybe you can remove these two members from it.
or maybe you can move some of their shared members (such as titlu, tiraj and...) to another class that is abstract, and then define Publicatie, Carte and Revista such that all of them inherit from the same parent class.
hope these work.

Upvotes: 1

Kerrek SB
Kerrek SB

Reputation: 476950

You can only inherit from a class that is a complete type. However, you don't need to have the nested class definition inside your ambient class definition. Instead, you can do it like so:

struct Foo
{
    struct Bar;

    Bar * p;

    int get();
};

struct Foo::Bar : Foo
{
    int zip() { return 4; }
};

int Foo::get() { return p->zip(); }

Upvotes: 0

Related Questions