Sign in to follow this  

Simple thing not compiling

This topic is 2336 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i am trying to compile this but i cant, and i dont see the mistake, i get linker error of function func in VS2010, and in code blocks i get "undefined reference". Only works if i put the definition inline in the class, or in the header, but anything i put in cpp file i get the same errors.

[code]

File: A.h

#ifndef _A_H_
#define _A_H_

template <class T> class A
{
public:

int n;
A() {};
~A() {};

T func(T n);
};

#endif

[/code]

[code]

File: A.cpp

#include "A.h"

template <class T> T A<T>::func(T n)
{
this->n = n;
return this->n;
}

[/code]




[code]

File: main.cpp

#include <iostream>
#include "A.h"

int main()
{
A<int> a;

std::cout << a.func(2);

return 0;
}


[/code]

Share this post


Link to post
Share on other sites
Nope,

any template class [b]cannot[/b] be defined in (i think it says:) more than one compile unit. That's like the major drawback in C++ templates if you ask me.

Just fit everything into your header file. Btw: you can of course still split the class declaration and the function definitions within the header file.

- Michael.

Share this post


Link to post
Share on other sites
Thank you, i thought there were the same as a non templated class, header and cpp files, and then the compiler knew how to manage them to have member functions always visible in the linker stage.

Share this post


Link to post
Share on other sites
The answers so far are incomplete.

You can split templates over multiple files. The problem is that if the template implementation isn't available where it is used, it will not be generated. A common solution is to have two header files for templates, the normal header file which declares the template, and a separate file which includes the implementation. The first header also #include<>s the second at the end.

Something like this:
[code]
#ifndef A_H
#define A_H

template <class T>
class A
{
public:
int n;
A() {};
~A() {};

T func(T n);
};

#include "a.impl.h"

#endif
[/code]
And a.impl.h would look like this:
[code]
#ifndef A_IMPL_H
#define A_IMPL_H

#include "A.h"

template <class T>
T A<T>::func(T n)
{
this->n = n;
return this->n;
}

#endif
[/code]
Another option is to use explicit instantiation. With explicit instantiation, you have a normal header file. In the template "source" file, you implement the functions. At the end you provide a set of explicit instantiations, one for each type that will be used.

This is generally used when the number of types used with the template is small.

Share this post


Link to post
Share on other sites
Well, to be pointlessly pedantic, the current version of the C++ standard does provide a method to put a template definition in a source file: the export keyword. The reason why this is pointless information is that pretty much only one compiler vendor, Comeau, ships a C++ tool chain that can use export - and even then it turns out that it's pretty much just easier to put them in headers.

Share this post


Link to post
Share on other sites

This topic is 2336 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this