c++如何用类Template创建对象
c++ how to create object with class Template
我有以下代码。
Main.cpp:
Warehouse<Base<int>> arm(1, 1, 1, 1);
arm.createSubBase(1,1,1);
仓库.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);
}
基本.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 );
。
函数参数需要是给出参数值的表达式,而不是像int
这样的类型名称。所以有问题的线路应该是
Base<T>* dN = new SubBase<T>(a,b,c,d);
将a
、b
、c
和d
替换为要传递给构造函数的任何参数。类似地,构造函数需要向其基类传递有效的参数(也需要使用正确的名称指定基类)。也许你想直接通过争论:
SubBase<T>::SubBase(int a, int b, int c, int d) : Base<T>(a,b,c,d) {...}
在类定义之后还缺少;
。
修复这些错误后,代码为我编译:http://ideone.com/mb0AOP
相关文章:
- 使用基类指针创建对象时,缺少派生类析构函数
- 如何创建对象函数指针C++映射?
- C++创建对象数组
- 在 C++ 的 Switch Case 中创建对象后对对象调用方法
- 如何获取在 main() 函数中构造的类的创建对象?
- 基于文件中的条目创建对象
- 错误:创建对象后无法分配区域
- C++ 通过输入创建对象
- 堆还是堆栈用于创建对象?
- 使用 C++ 创建对象数组
- 使用unique_ptr创建对象
- C++递归地在类构造函数中创建对象
- 通过向构造函数其他对象引用页面来创建对象
- ReactiveX (rx) - 在对象上应用可观察对象,而不是在可观察对象中创建对象
- 如何在OSX上正确创建C++对象文件(.o)
- 编译器是否会创建vtable,而不考虑在c++中创建对象
- 创建对象并防止被破坏
- 在C++中,友元类可以从友元类创建对象吗
- 只在堆中创建C++对象
- c++如何用类Template创建对象