C ++ templates error

I'm trying to write a simple vector class with templates, but when I split it into a .h and .cpp file, I get the following errors:

undefined reference to vector<int>::vector()' undefined reference to

vector :: add (int) 'undefined reference to vector<int>::add(int)' undefined reference to

vector :: remove (int)'

code: http://pastie.org/623584

+2


source to share


4 answers


Either follow Naveen's advice and keep it in the header or add

template class vector<int>;

      



To your .cc

file. But you have to create an instance for all possible template arguments.

+3


source


The reason for your problem is the C ++ file organization.

About titles and sources

Sources are files that are compiled into binary "object files" and then linked together to create the final binary (either a library or an executable).

But for one source using code defined in another source, they have to share "declarations" of the code. Thus, the declaration must be placed in common files, which we will call header files.

Typically, the code in the headers is not "true code", but just a declaration of the existence of that true code.

This is what you did with your custom vector: put the declaration in the header and the implementation in the source, and include the header in main.

About embedding

Now you can put some "true code" in headers, usually by prefixing it with the keyword "inline".

Again, this is not real code: the compiler and linker will determine what to do with it, but it can:

  • Move the function code into an "object file" and add all other "object files" there.
  • Introduce code using built-in function
  • Two solutions above at the same time

But the built-in function can disappear from the binary if not used.

About templates



Templates are somewhat like a declaration: they are not "true code" but "potential code". This is even more "potential" than inline code, because the template code will need to be generated for the template parameters to be correct.

As with any other code, in order to use the template code, your source file must have access to the declaration. But in this case , the template declaration is both a "potential declaration" and a "potential implementation" that the compiler will use for the correct types you are using.

There are other ways to work with templates, but this will always work.

One last tip

When working with templates (or inline code) it is very convenient to split the header into multiple files (for example, when working with circular declaration dependencies).

Example:

file: MyObject.hpp

#include <MyObject_header.hpp>
#include <MyObject_source.hpp>

      

file: MyObject_header.hpp

class MyObject
{
   // Etc.
} ;

      

file: MyObject_source.hpp

#include <MyObject_header.hpp>

MyObject::MyObject()
{
   // etc.
}

// etc.

      

+1


source


You cannot separate the declaration and implementation of a template class into .h and .cpp. It should be there in .h. When vector is in main (), the compiler tries to instantiate the vector class. To do this, he needs to know the complete implementation of the class, so you need to write the entire templated class in .h itself.

0


source


There are several ways to avoid such linking problems, it should be very interesting for you to read http://www.parashift.com/c%2B%2B-faq-lite/templates.html#faq-35.15

He explains various solutions to you. If you are not creating a template template you should put definitions and implementations in .h , but if you need to add something like

template class vector<int>;

      

or (and)

template class vector<double>;

      

in the .cpp , according to the templates you create.

0


source







All Articles