두 개의 C++ 템플릿이 있습니다. h와 cpp 파일을 갖고 싶습니다. 하나의 템플리트에서만 작동하지만 두 개의 다른 템플리트에서는 작동하지 않습니다. 나는 GCC이 오류가 나타날 수템플릿 인자 선언
여기../Temp1.cpp:16:17: error: definition of ‘Name::Temp1<T>::Temp1()’ is not in namespace enclosing ‘Name::Temp1<T>’ [-fpermissive]
../Temp1.cpp:22:18: error: definition of ‘Name::Temp1<T>::~Temp1()’ is not in namespace enclosing ‘Name::Temp1<T>’ [-fpermissive]
make: *** [Temp2.o] Error 1
TEMP1 헤더 코드 :
#ifndef TEMP1_H_
#define TEMP1_H_
namespace Name {
template<class T>
class Temp1 {
public:
Temp1();
virtual ~Temp1();
};
#include "Temp1.cpp"
} /* namespace Name */
#endif /* TEMP1_H_ */
TEMP1의 CPP :
#ifndef TEMP1_CPP_
#define TEMP1_CPP_
#include "Temp1.h"
namespace Name {
template<class T>
Temp1<T>::Temp1() {
// TODO Auto-generated constructor stub
}
template<class T>
Temp1<T>::~Temp1() {
// TODO Auto-generated destructor stub
}
} /* namespace Name */
#endif
TEMP2 헤더 :
#ifndef TEMP2_H_
#define TEMP2_H_
#include "Temp1.h"
namespace Name {
template<class T>
class Temp2: public Temp1<T> {
public:
Temp2();
virtual ~Temp2();
};
#include "Temp2.cpp"
} /* namespace Name */
#endif /* TEMP2_H_ */
TEMP2의 CPP :
#ifndef TEMP2_CPP_
#define TEMP2_CPP_
#include "Temp2.h"
namespace Name {
template<class T>
Temp2<T>::Temp2() {
// TODO Auto-generated constructor stub
}
template<class T>
Temp2<T>::~Temp2() {
// TODO Auto-generated destructor stub
}
} /* namespace Name */
#endif
템플릿은 선언 된 위치에 구현해야합니다. –
헤더에있는 템플릿은 헤더 작성에 대해 알고있는 것에 위배됩니다. cpp가 아닌 헤더에 처리하거나 넣기 위해 예측하는 각 전문 분야에 대해 제공해야합니다. – ChiefTwoPencils
.cpp 파일을 .h 파일에'포함 '시키지 마십시오. –