Home > Back-end >  Using class type as template argument type when create class definition
Using class type as template argument type when create class definition

Time:01-06

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.

CodePudding user response:

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.

  •  Tags:  
  • Related