Steve Jackson


Really new to C++ so please be gentle with me on this one. When I try to compile the following code, I get 4 unresolved link errors. Note that I have three files in my project, "Sample.h", "Sample.cpp", and "Main.cpp".

//Sample.h

template <class t>

class Sample

{

public:

Sample() ;

~Sample() ;

} ;

// Sample.cpp

#include "Sample.h"

template <class t>

Sample<t>::Sample()

{

}

template <class t>

Sample<t>::~Sample()

{

}

//Main.cpp

#include "Sample.h"

void main()

{

Sample<int> Sampleint;

Sample<float> Samplefloat;

}

An example error is as follows;

Error 1 error LNK2019: unresolved external symbol "public: __thiscall Sample<int>::~Sample<int>(void)" ( 1 $Sample@H@@QAE@XZ) referenced in function _main Main.obj

Thanks




Re: Visual C++ General unresolved link when using template class

Prasad Somwanshi

It is beacause, in some cpp files in which this header is include, its definition is not visible. And compiler instantiates its defintions per translational units.

Solution to this problem is, have function declarations and defintions in same header file.





Re: Visual C++ General unresolved link when using template class

Steve Jackson

Thanks for the reply. It didn't seem to help though. To be sure I understood you correctly, you suggested moving everyting from Sample.cpp to Sample.h - below the class. I did that - same error occurs.






Re: Visual C++ General unresolved link when using template class

Prasad Somwanshi

It shouldn't.

I hope, your class declaration and defintion are in same file, and you are including that header in main.cpp.

 





Re: Visual C++ General unresolved link when using template class

Steve Jackson

Thanks again for your reply. I now only have two files in my project, Main.cpp, and Sample.h. Below is the code in both files;

// ************* Sample.h ************

#pragma once

template <class t>

class Sample

{

public:

Sample() ;

~Sample() ;

} ;

template <class t>

Sample<t>::Sample()

{

}

template <class t>

Sample<t>::~Sample()

{

}

// ************* Main.cpp ************

#include "Sample.h"

void main()

{

Sample<int> Sampleint;

Sample<float> Samplefloat;

}






Re: Visual C++ General unresolved link when using template class

Prasad Somwanshi

Yes, this code is working fine. I've tested on my side,too. I'm sure, you missing something trivial.



Re: Visual C++ General unresolved link when using template class

Steve Jackson

 

Thanks again Prasad;

Really though, it can't get much simpler than the code above, and it is a direct copy and paste from my project. Thank you for trying it on your end. Something tells me that I have a wrong setting in visual studio, causing it to look for something that it shouldn't.

If I take out all of the reference to 'template', it compiles fine.

 

 






Re: Visual C++ General unresolved link when using template class

Prasad Somwanshi

I advice you to start with fresh console application, and follow step by step.

I dont think , its because of some setting.





Re: Visual C++ General unresolved link when using template class

Steve Jackson

Nope, started from scratch and it doesn't work. It compiles just fine as long as I don't include any template statements.






Re: Visual C++ General unresolved link when using template class

Steve Jackson

Turns out that you are correct Prasad, not only does all the code have to be in the same file (.h), but it would appear (at least for me anyway) that all of the code has to be in-line, within the brackets that define the class. Once I did that, it works for me now.

Thanks.






Re: Visual C++ General unresolved link when using template class

Wil R.

You shouldn't have to go quite that far. It works even if the body of the code for the template class follows the closing brace of the class definition, so long as they are in the same file. I tested that with your Sample template, and it worked.

Wil





Re: Visual C++ General unresolved link when using template class

Steve Jackson

Thanks Wil,

You are correct, I have sense been able to get it to work simply by ensuring that it was in the same .h file. I'm not sure why I didn't get it to work on this particular project - must have missed something simple.

SteveJ