c ++如何使用类Template创建对象

时间:2014-10-25 14:44:21

标签: c++ templates inheritance

我有以下代码。

Main.cpp的:

Warehouse<Base<int>> arm(1, 1, 1, 1);
arm.createSubBase(1,1,1);

Warehouse.h:

private:
 vector<Base<T>*> whouse;
public :
 void createSubBase(int, int, int);

template <class T> 
void Warehouse<T>::createSubBase(int,int,int) {
  Base<T>* dN = new SubBase<T>(int,int,int,int); ***<-ERROR MESSAGE:" in file included from"***
     whouse.push_back(dN);
}

Base.h:

template <class T>
class Base {
private:
 int I,a,b,c;
public :
  Base(int,int,int,int);
}

template <class T>
Base<T>::Base(int i, int a, int b, int c) {
    this -> I = i;
    this -> a= a;
    this -> b= b;
    this -> c = c;
}

SubBase.h:

template <class T>
class SubBase: public Base<T> {
public:
  SubBase(int, int, int,int);
}
template <class T>
SubBase<T>::SubBase(int, int, int , int) : Depositos<T>(int,int,int,int) {...}

有谁知道为什么我收到此错误消息?我不明白为什么不让我创建Base<T> * b = new subbase<T> ( int , int , int );

1 个答案:

答案 0 :(得分:1)

函数参数需要是赋予参数值的表达式,而不是像int这样的类型名称。所以有问题的行应该是

Base<T>* dN = new SubBase<T>(a,b,c,d);

abcd替换为您要传递给构造函数的任何参数。类似地,构造函数需要将有效参数传递给其基类(也需要使用正确的名称指定)。也许你想直接传递参数:

SubBase<T>::SubBase(int a, int b, int c, int d) : Base<T>(a,b,c,d) {...}

您在课程定义后也缺少;

修复这些错误后,代码会为我编译:http://ideone.com/mb0AOP