Template issue causes linker error (C++)
You have to have your template definitions available at the calling site. That means no .cpp
files.
The reason is templates cannot be compiled. Think of functions as cookies, and the compiler is an oven.
Templates are only a cookie cutter, because they don't know what type of cookie they are. It only tells the compiler how to make the function when given a type, but in itself, it can't be used because there is no concrete type being operated on. You can't cook a cookie cutter. Only when you have the tasty cookie dough ready (i.e., given the compiler the dough [type])) can you cut the cookie and cook it.
Likewise, only when you actually use the template with a certain type can the compiler generate the actual function, and compile it. It can't do this, however, if the template definition is missing. You have to move it into the header file, so the caller of the function can make the cookie.
Linker error when using a template class?
Due to a weirdness in C++'s compilation model, you cannot separate out .h and .cpp files very cleanly for template classes. Specifically, any translation unit (C++ source file) that wants to use a template class has to have access to the entire template definition. This is a strange quirk of the language, but unfortunately it's here to stay.
One option is to put the implementation up in the header file rather than in the source, then to not have a .cpp file at all. For example, you might have this header:
#pragma once
#ifndef hijo_h
#define hijo_h
template <class A>
class hijo
{
public:
hijo(void);
~hijo(void);
};
/* * * * Implementation Below This Point * * * */
template <class A>
hijo<A>::hijo(void)
{
}
template <class A>
hijo<A>::~hijo(void)
{
}
#endif
Hope this helps!
C++ linker error when calling template function that is defined in header
The problem seems to be that you wrote
std::shared_ptr<T> ptr(T);
instead of
std::shared_ptr<T> ptr(value);
Explicit instanciation of template linker error
Since you are using a nonstandard extension, .tpp
, you may want to tell the compiler that you are working with C++ code, g++ -x c++
.
c++ Weird linker error happens when passing some arguments to a template
As I was trying to reproduce the problem, I found out where the problem is.
The problem here is constructor overload resolution chooses a constructor have not implemented yet.
wyf::vector<int> v1(10, 20);
The call above is supposed to call the following constructor through a implicit conversion from int to size_type(std::size_t) conducted on 10:
vector(size_type n, const_reference val, const allocator_type& alloc = allocator_type());
But instead, compiler sees a better match, and chooses:
template<typename IterTp>
vector(IterTp beg_, IterTp end_, const allocator_type& alloc = allocator_type());
and instantiates wyf::vector (int, int, const std::allocator&) with IterTp being int. Since I have not implemented that constructor yet, the code compiles but fail to link. The following code succeeds to link:
wyf::vector<int> v3(static_cast<std::size_t>(10), 20);
wyf::vector<int> v4(10.0, 20);
wyf::vector<int> v5(10, 20.0);
becasue the 2 aurguments are of two different types. So the compiler cannot deduce the type of IterTp for the template version constructor, and the non-template version is chosen, which is the code works as I expected.
C++ linker errors after declaring a template class
It is important to learn how to read the error messages from the compiler and the linker. In this case the linker is complaining:
Error 2 error LNK2019: unresolved external symbol "public: virtual __thiscall ResourceManager::~ResourceManager(void)" (??1?$ResourceManager@VFont@sf@@@@UAE@XZ) referenced in function "public: virtual __thiscall FontManager::~FontManager(void)" (??1FontManager@@UAE@XZ) FontManager.obj
Where is the definition of your destructor?
The other errors are a bit harder to explain with the code that you posted. Is the definition of the get
member function in the header that declares the template?
Linker error 'unresolved external symbol' : working with templates
You cannot split templates into .h and .cpp files - you need to put the complete code for the template in the .h file.
Related Topics
Why Is Stack Memory Size So Limited
C++11: I Can Go from Multiple Args to Tuple, But How to Go from Tuple to Multiple Args
When Is It Best to Use the Stack Instead of the Heap and Vice Versa
Does There Exist a Static_Warning
How to Use Std::Sort to Sort an Array in C++
When G++ Static Link Pthread, Cause Segmentation Fault, Why
How to Properly Replace Global New & Delete Operators
What Expressions Yield a Reference Type When Decltype Is Applied to Them
Does "Const" Just Mean Read-Only or Something More
How to Implode a Vector of Strings into a String (The Elegant Way)
What Does the Standard Library Guarantee About Self Move Assignment
How to Force Linker to Use Shared Library Instead of Static Library