C++ templates, undefined reference - c++

I have a function declared like so:
template <typename T>
T read();
and defined like so:
template <typename T>
T packetreader::read() {
offset += sizeof(T);
return *(T*)(buf+offset-sizeof(T));
}
However, when I try to use it in my main() function:
packetreader reader;
reader.read<int>();
I get the following error from g++:
g++ -o main main.o packet.o
main.o: In function `main':
main.cpp:(.text+0xcc): undefined reference to `int packetreader::read<int>()'
collect2: ld returned 1 exit status
make: *** [main] Error 1
Can anyone point me into the right direction?

You need to use the export keyword. However, I don't think G++ has proper support, so you need to include the template function's definition in the header so the translation unit can use it. This is because the <int> 'version' of the template hasn't been created, only the <typename T> 'version.'
An easy way is to #include the .cpp file. However, this can cause problems, e.g. when other functions are in the .cpp file. It will also likely increase the compile time.
A clean way is to move your template functions into its own .cpp file, and include that in the header or use the export keyword and compile it separately.
More information on why you should try and put template function definitions in its header file (and ignore export altogether).

The problem is that a function template is not a function. It's a template for creating functions as needed.
So for a template to work, the compiler intuitively needs two pieces of information: The template itself, and the type that should be substituted into it.
This is unlike a function call, which the compiler can generate as soon as it knows that the function exists. It doesn't need to know what the function does, just that it looks like void Frobnicate(int, float), or whatever its signature is.
When you declare the function template without defining it, you're only telling the compiler that such a template exists, but not what it looks like. That's not enough for the compiler to be able to instantiate it, it has to be able to see the full definition as well. The usual solution is to put the entire template in a header that can be included where needed.

The best practice with template functions is to define them in header files. They are created at compile time so compiler has to have definition around to do so.
When export for templates would be more supported this wouldn't be the case though but right now it still hardly can be used.

Is their any compiler support template separate compilation?
As I know the common practice is declare and implement template functions in the header file

Related

undefined reference to void "function" C++

I got the following error:
features.cpp:(.text+0x4f6): undefined reference to `void Convolve<float>(CImageOf<float>, CImageOf<float>&, CImageOf<float>)'
So, in my features.cpp file, I added the following header file, with the content:
template <class T>
void Convolve(CImageOf<T> src, CImageOf<T>& dst,
CFloatImage kernel);
I'm compiling this with GCC on ubuntu 64bit. I realize that the error above is because what is in my header does not match the reference, but that's because of the template, and so I shouldnt be having this problem right? What else is going on?
I double checked my calls to convolve, and actually this is the types I'm using :
Convolve(CFloatImage, CFloatImage, CFloatImage);
I can't actually find any reference to the error above
If you put a function template into a .cpp file and use it in a different translation unit, you need to explicitly instantiate the template, e.g.:
template
void Convolve<float>(
CImageOf<float> src,
CImageOf<float>& dst,
CFloatImage kernel);
Basically, the compiler can instantiate a templte only when it has seen its definition. If you want the template to be instantiated automatically, you need to define it in a header.
As turned out in a chat with the OP the problem was probably in a Makefile that did not link correctly.
There was no definition but a declaration of the template in the .h file (as some of you suggested). I think the OP confused the terms.
However there was a definition of the template in the .cpp file along with a (non template) function definition that instantiated Convolve<float> (and others).
So to repeat it: The code was correct but the build routine was not.

Include .cpp file? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why can templates only be implemented in the header file?
I've been trying around with C++ recently.
At the moment I'm trying to program something I'm sure everone has done at least once: A simple LinkedList class.
The code is done, but I'm somehow failing to compile it. I've been googling and it seems like I'm linking the object files wrong. That's what my code basically looks like:
test.cpp
#include "linkedlist.h"
int main()
{
LinkedList<int> list;
// do something
}
linkedlist.h
template <typename T>
class LinkedList
{
// a lot of function and variable definitions
}
Then there's a .cpp file called linkedlist.cpp which contains all the actual code of the LinkerList class. When trying to compile test.cpp using the following command:
g++ ..\src\test.cpp
I'm getting told that there's an undefined reference to 'LinkedList::LinkedList()'. So I've been thinking that it's being linked wrong as there's more than one .cpp file, so I tried it like this:
g++ -c -Wall -O2 ..\src\test.cpp
g++ -c -Wall -O2 ..\src\linkedlist.cpp
g++ -s test.o linkedlist.o
However, this doesn't change anything. The error messages stay the same.
I've been trying to find some information on the internet, however, it didn't really work out.
You're creating a class template, which has the important caveat that all variable definitions must be placed in the header file so that they're accessible to all translation units during compilation.
The reason is the way that templates work. During compilation, the compiler instantiates template classes based on your class template definition. It isn't enough for it to have access to only the declarations or signatures: it needs to have the entire definition available.
Move all of your method definitions out of the .cpp file and into the .h file, and everything should be fine (assuming that you have, in fact, provided a definition for the default constructor!).
Alternatively, you might be able to get around this by explicitly telling your compiler to instantiate the appropriate template class using something like template class LinkedList<int>, but this really isn't necessary in such a simple case. The primary advantage of this over including all of the definitions in the header file is that it potentially reduces code bloat and speeds up compilation. But it might not be necessary at all, as compilers have gotten a lot smarter at applying appropriate optimizations.
You have split up your class LinkedList and put the declaration in a header and the defintion in a source-file. This does not work for a template. Both have to be in the header.
Template class is like a definition of a rule by which to construct an actual class. The actual classes are created (instantiated) by the compiler wherever there is instantiation of template class. For this reason all the code of template class must be visible to a compiler at that point, and that's why you have to write all the template functions and methods in a header files - because you only include headers to cpp files, only headers are visible to a compiler at that point. You don't include cpp files to another cpp files, and you should never do that.
Correct me if my understanding is wrong.
P. S.
Does C++11 address this issue?
Since LinkedList is a class template, the definitions of its member functions ought to go in a header.
(They don't have to be, strictly speaking, but this is the easiest way to get around the complexities of template instantiation without going totally off-piste with inclusion conventions.)
Oh, and you'd need to take your second build approach anyway; you should link together all .cpp files, or use the -c switch to compile one at a time then link the results together later.
there's an undefined reference to 'LinkedList::LinkedList()'
Have you checked to see if your LinkedList class has a constructor?

C++ linking for an overloaded << function (logger class)

I'm currently writing a logger for my C++ project (I need it to have near to no dependences, so I don't want to take a prewritten one). It contains this function:
template <typename T>
Logger& Logger::operator<<(T f) {
m_file<<f;
return *this;
}
The logger.cpp code compiles, but when I call the log functions in my main.cpp, I have this compiler error:
/home/tuxer/prog/cpp/PRay/server/src/main.cpp:110: undefined reference to `Logger& Logger::operator<< <int>(int)'
for this line of code :
log<<lul; (lul being a int variable equals to 2)
The logger.o file is correctly included, as the Logger::init() function works properly and doesn't raise any linking error.
Thanks :)
Since you have non-inline templates, you need to force instantiation. See for example How do I force a particular instance of a C++ template to instantiate?.
The simple thing to do is to put the Logger::operator<< template in the header file. The compiler will automatically instantiate the versions it needs, and the linker will remove the duplicates (well, at least the ones that weren't inlined).
You shouldn't need to force instantiation unless your linker is old (e.g. gcc 2.7 or earlier).
This is the general rule with template code: put the definitions in the header file unless you have a good reason not to.
See Why can templates only be implemented in the header file? as well.

Making a generic function in C++

I try to make a generic function in C++, that creating an array of a type which depends on usage (when calling it), like array of int or char.
This is the code in my header file:
template<class T>
T** makeArray(...);
This is in the .cpp file:
template<class T>
T** ClassA::makeArray(...){
//...
}
And this is how I call it in the main file:
char** charArr = cg.makeArray<char>(...);
//...
int** intArr = cg.makeArray<int>(...);
But when compiling, I get these errors:
undefined reference to `char** ClassA::makeArray<char>( ...
undefined reference to `int** ClassA::makeArray<int>( ...
What do the errors mean and how do I fix it?
You have to include the entire body of a C++ template function in the header.
This is because of the way that C++ templates work: template parameter substitution happens only when you actually use the template, and the entire template definition has to be visible at that time. Another way of looking at it is to say that you cannot compile and link to a templateFunction<T>—you can only compile a template function once it's been instantiated as a templateFunction<char> or whatever. For this reason, you cannot put template function definitions in .cpp files, but rather have to put them in .h files.
The compiler needs to have access to the entire template definition (not just the signature) in order to generate code for each instantiation of the template, so you need to move the definitions of the functions to your header.
This is called the inclusion model. Also read this FAQ
In general, you cannot have template definitions in a source file; they should be in the header file so that the compiler can see them.
This comes up over and over again here, so I can't be bothered to give the full explanation again; I recommend you read this from the C++ FAQ: Why can't I separate the definition of my templates class from its declaration and put it inside a .cpp file?.
[Incidentally, why are you returning a pointer-to-pointer? A pointer should be enough...]
If your code is using the template on a diferent cpp file, the template should go on the header file.

Templates and headers question

The compiler says it can't find the reference for the function when I do this:
// link.h
template <class T>
T *Link(T *&, T *(*)())
// link.cpp
template <class T>
T c:Link(T *&ChildNodeReference, T *(*ObjectCreator)()){
}
If I implement inside the class on the header it goes smoothly.
Please, I will work on the header until someone enlightens me about this.
There are somethings in C++ that are weirdly annoying. I know, there is a reason for this and etc. Even so, can't the compilers help you out about it -_-"
Templates are essentially semi-type-safe macros, hence the limitation.
Normal (non-template) functions can be compiled to native code residing object/library files, and then referenced with only a prototype available in the header, solely because there's only a single version of such a function.
With templates, C++ compiler has to compile each instantiation of the function separately. Obviously, it cannot do it "in advance", because the set of types for which you can instantiate the function is effectively unbounded (you can always define a new type in your code before calling the function). In fact, two instantiations of the same function template can be completely different. Consider this extreme case:
struct t1 {
template <int>
struct a {};
};
struct t2 {
enum { a = 123 };
};
enum { b = 456, c = 789 };
template <class T>
void foo() {
T::a<b>c;
}
Now if we call foo<t1>(), the statement inside it is a local variable declaration, because t1::a is a class template:
T::a<b> c;
But if we call foo<t2>(), the statement inside is an expression, because t2::a is an integral constant:
(T::a < b) > c;
This is just to show that compiler cannot meaningfully "compile" a template; it really has to mostly preserve the tokens.
Now, all that said, ISO C++ does actually provide the ability to separate declaration and definition of templates, so that they can be treated as normal functions, with declarations in .h files, and definitions in .cpp files. This is called "export templates", because you have to precede both declaration and definiton with keyword export:
// link.h
export template <class T>
T *Link(T *&, T *(*)());
// link.cpp
export template <class T>
T *Link(T *&ChildNodeReference, T *(*ObjectCreator)()) {
}
This is, however, a controversial feature of the Standard because of very high burden on implementation, and most popular implementations refuse to implement it; notably, g++, MSVC, and C++Builder do not implement it. The only compiler I know of that supports it is Comeau C++.
Programming non-template code or non-inlined functions in headers is a Bad Thing™. The reason you have cpp files is to prevent redefinition of the same function code more than once, amongst other things.
The difference with templates is that the compiler practically doesn't touch them until your code instantiates a specialisation of that template which is why they need to have their source inside the header.
When the compiler finds an instantiation of a template specialisation (say List<int>), it goes back to the included template code and compiles it with that specialisation, which is why you don't have issues with redefinition of function code.
What you don't seem to understand is that this doesn't apply to non-templated code. All non-template code is compiled as normal and thus CPP files are needed to only define the code once then link it all together.
If you define functions inside a header, your linker will not link the compiled translation units because they have defined the same function more than once.
Templated implementations (not only definitions) have to be available at compile time.
So, the full template code is normally put in the header file.
Template code must be in the header. Sorry, I completely missed that! (and I thought I'd been coding C++ for years :P)
You forgot the * for the return type. So implementation is not matching definition. Add it and it should work:
T *c:Link(T *&ChildNodeReference, T *(*ObjectCreator)())
{
}
Implementation must be too in the header file under the class definition in order to be available at compile time.