PDA

View Full Version : problem with using template classes



arbi
1st September 2010, 07:43
hi,

i am trying to derive new classes from a template base class. I simplified my code as below just to
show the problem.




template<class T>
class BaseClass
{
public:
BaseClass(T&);
private:
};

//.cpp implementation

template<class T>
BaseClass<T>::BaseClass<T>(T& iT)
{
}


and my derived class as:



class DerivedClass : public BaseClass<QString>
{
public:
DerivedClass(QString);
~DerivedClass();
private:
};

//.cpp implementation

DerivedClass::DerivedClass(QString st)
: BaseClass<QString>(st)
{
}

DerivedClass::~DerivedClass()
{
}



when i try to compile this code it gives link errors as below

DerivedClass.obj : error LNK2019: unresolved external symbol "public: __thiscall BaseClass<class QString>::BaseClass<class QString>(QString&)" (??0?$BaseClass@VQString@@@@QAE@XZ) referenced in function "public: __thiscall DerivedClass::DerivedClass(QString)" (??0DerivedClass@@QAE@XZ)


it doesn't generate any error when i do not implement any constructor in the base class. and do not call the base constructor, but i want to implement some code in the base class' constructor.
so, what is the problem with this?

thanks

aamer4yu
1st September 2010, 07:54
As far as I know,, In C++ , if you are defining a parametrized constructor, you need to define a default constructor too.
Thats the problem in your code I guess

wysota
1st September 2010, 08:24
Template classes have to be defined completely in header files, they can't have .cpp files. Move the constructor body to the header file.

arbi
1st September 2010, 08:26
nope. that is not required in C++. Anyway, i added default constructor to the class but same error generated again.

may be deriving new class from template classes does not work but i dont know for sure?

arbi
1st September 2010, 13:29
Template classes have to be defined completely in header files, they can't have .cpp files. Move the constructor body to the header file.

thank you, it worked. I learned something new.