Ok guys... I have following class
#include <functional>
template <typename TValue, typename TPred = std::less<TValue>>
class BinarySearchTree {
struct TNode {
TValue value;
TNode *pLeft;
TNode *pRight;
};
public:
BinarySearchTree();
~BinarySearchTree();
. . .
private:
TNode *pRoot;
. . .
};
then in my .cpp file i defined the ctor/dtor like this:
template <typename TValue, typename TPred>
BinarySearchTree<TValue, TPred>::BinarySearchTree() : pRoot(0) {}
template <typename TValue, typename TPred>
BinarySearchTree<TValue, TPred>::~BinarySearchTree() {
Flush(pRoot);
}
my main function:
int main() {
BinarySearchTree<int> obj1;
return 0;
}
and i get following linkage error:
public: __thiscall BinarySearchTree<int,struct std::less<int>>::BinarySearchTree<int,struct std::less<int> >(void)
i tried to put the constructor definition into the header file and i get no error. only if i try to define it in the cpp file.
As long as you are satisfied with automatic type inference, you can use a template constructor (of a non-template class). @updogliu: Absolutely. But, the question is asking about "a template constructor with no arguments" If there are no function arguments, no template arguments may be deduced.
A class can have multiple copy constructors, e.g. both T::T(const T&) and T::T(T&). If some user-defined copy constructors are present, the user may still force the generation of the implicitly declared copy constructor with the keyword default .
No copy constructor is automatically generated.
The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to − Initialize one object from another of the same type. Copy an object to pass it as an argument to a function.
In this article, I am going to discuss Template Classes in C++. Please read our previous article where we discussed C++ Class and Constructors. The C++ programming language supports generic functions and generic classes. Generic functions are called template functions and generic classes are called template classes.
In C++, we can use multiple template parameters and even use default arguments for those parameters. For example, template <class T, class U, class V = int> class ClassName { private: T member1; U member2; V member3; ... .. ... public: ... .. ...
A class template starts with the keyword template followed by template parameter (s) inside <> which is followed by the class declaration. template <class T> class className { private: T var; ... .. ... public: T functionName(T arg); ... .. ... };
Class template instantiation. No code is generated from a source file that contains only template definitions. In order for any code to appear, a template must be instantiated: the template arguments must be provided so that the compiler can generate an actual class (or function, from a function template).
Don't define templates in the cpp file, but put the implementation of the functions in the header file and leave your main function as it is. Templates get inlined by default. Therefore they are not visible to the linker. And the file that contains main() cannot see the definition of the templates. Hence the error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With