2014-10-25 2 views
0

다음 코드가 있습니다.C++ 클래스 템플릿을 사용하여 객체를 만드는 방법

하여 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

함수 인수는 int과 같은 형식 이름이 아닌 인수의 값을 제공하는 식이어야합니다. 그래서 문제가있는 줄은

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

이 생성자에 전달할 원하는 인수 a, b, cd를 교체해야합니다. 비슷하게, 생성자는 기본 클래스 (올바른 이름으로 지정되어야 함)에 유효한 인수를 전달해야합니다. 또한 클래스 정의 후 ;이 누락

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

: 아마도 당신은 직선을 통해 인수를 전달하고자합니다.

이 오류를 수정하면 코드가 나를 위해 컴파일됩니다. http://ideone.com/mb0AOP

관련 문제