我想知道产生此错误的确切编译器行为。具有非模板基类的模板类
看看这段代码。
class Base_class
{
public:
Base_class();
};
Base_class::Base_class()
{
//Here it says multiple definitions (If I define the contructor outside)
//If I define this inside class no error
//Or if I make the base class templated no error
//Also if this is in .cpp it works.
}
template<typename T>
class Temp_derived_class:Base_class
{
public:
Temp_derived_class();
int *i;
};
template<typename T>
Temp_derived_class<T>::Temp_derived_class()
{
i = new int[5];
}
这说多定义(如果我定义外的构造器) 如果我定义这个类里面没有错误 或者,如果我做模板没有错误的基类 此外,如果这是的.cpp它的工作原理。
干杯, CB
它就像使用任何非内联非模板函数一样:如果在头文件中定义它,会出现多个定义错误。这里的模板只是一个分心。 – juanchopanza