Belisko Marek
Belisko Marek

Reputation: 48

Using class type as template argument type when create class definition

I have a base class BaseCmd like:

template<typename T>    
class BaseCmd {
public:
private:
  T m;
};

and then derived class Cmd1:

class Cmd1 : public BaseCmd<Cmd1::A> {
public:
  struct A {
    int c, d;
  };
};

but I'm getting error:

error: incomplete type ‘Cmd1‘ used in nested name specifier

Is it even possible to define it like that? Thanks.

Upvotes: 1

Views: 48

Answers (1)

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122178

You cannot have a member of type Cmd1::A before Cmd1 is complete. The simple fix is to define A outside of Cmd1. However, if for whatever reason you want to define A inside Cmd1 you can add a layer of indierction like this:

template<typename T>    
class BaseCmd {
public:
private:
  T m;
};


class Cmd1 {
public:
  struct A {
    int c, d;
  };
  struct impl : public BaseCmd<A> {};
};

Cmd1::impl can inherit from A because As definition is complete by the time BaseCmd<A> is used as base class.

Upvotes: 1

Related Questions