Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template class constructor [duplicate]

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.

like image 987
xlw12 Avatar asked Jun 03 '13 18:06

xlw12


People also ask

Can you template a constructor?

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.

Can there be multiple copy constructor?

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 .

Are copy constructor automatically generated?

No copy constructor is automatically generated.

What are copy constructors in C++?

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.

What is a template class in C++?

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.

How to use multiple template parameters in C++?

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: ... .. ...

How do you create a class template in Python?

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); ... .. ... };

How do you instantiate a class template?

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).


1 Answers

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.

like image 184
Ralph Tandetzky Avatar answered Oct 11 '22 18:10

Ralph Tandetzky