Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Really quick question: Templates 1

Status
Not open for further replies.

djones7936

Programmer
Jul 20, 2002
24
US
Does anybody know why template code will not compile unless the class declaration and actual class code is in the same file? In other words, if I separate the class into two files .h(header) and .cpp (source file), the code does not run.

Here is an example of that I'm talking about. I've tried the code with VC++ 6.0 and 7.0, as a Blank Windows Console program.

Code:
// In "DSRGB24.h"
template <class T>
class CRGB24
{
private:
	T m_R;
	T m_G;
	T m_B;
public:
	CRGB24();
};

// In &quot;DSRGB24.cpp&quot;
#include &quot;DSRGB24.h&quot;

template <class T>
CRGB24<T>::CRGB24()
{
	m_R = 0;
	m_G = 0;
	m_B = 0;
}

//In main.cpp
#include &quot;DSRGB24.h&quot;

int main()
{
	CRGB24<int> iRGB1;
	CRGB24<int> iRGB2;

	return 0;
}
My error message is:
Code:
Compiling...
DSRGB24.cpp
Linking...
main.obj : error LNK2001: unresolved external symbol &quot;public: __thiscall CRGB24<int>::CRGB24<int>(void)&quot; (??0?$CRGB24@H@@QAE@XZ)
trash___Win32_Debug/trash.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

trash.exe - 2 error(s), 0 warning(s)
I can't believe this isn't working! I made a really elaborate class using template but it wouldn't compile unless all the code for the class was in one file. I don't understand why this code wouldn't work.

Does anyone have insight as to why C++ was designed this way, or if there is something I'm overlooking?

Thanks, -Dave
 
The reason it won't compile is because your main.cpp only knows about DSRGB24.h. The header file has no information about the DSRGB24.cpp file and therefore your methods are not exposed to it. The easiest solution is to include the DSRGB24.cpp instead of the DSRGB24.h. You might also want to surround the header and cpp file with #indef, #define, #endif so that the program doesn't attempt to define the template more than once if it it being used in multiple places.

HTH,
JC
 
Thank you Mongoose,

The .cpp extension works.

That's weird. When I deal with classes without templates, (and as with most cases in C++) files are #included with &quot;.h&quot; extension. The class's source code is in a .cpp file, but it is still accessible from other files (such as main.cpp) without any problems. Can you tell me why the usual method fails when using templates, but does fail in most other cases? How is C++ designed to cause such behavior?

Is including &quot;.cpp&quot; files rather than &quot;.h&quot; files a standard practice when dealing with templated classes?

In my actual program, I did have the # directives in place, but that is a good thing to point out for anyone else reading. I wish I knew that when I was designing the first version of my program! [ponder]

Thanks,
-Dave
 
I am merely a C++ hack - but I beleive if you wanted to expose only the .h file so it in essence defines the interface to your methods, and the user would only see the .h file and not the implementation then you could create a .lib file with the implementation and then just include the .lib in your project settings. There are probably other ways to direct the compiler to link the .cpp with the .h during the build as well so that the initial .obj file would contain both files. But like I said this is above my skill level. Glad it worked though.

JC
 
I see,

Correct me if I'm wrong,
When I include <.h> (standard .h files), the .h file refers to already compiled code which is stored in a &quot;.lib&quot; file.

When I include &quot;.h&quot; (my custom made .h files), it refers to already compiled code in an .obj file.
(The corresponding &quot;.cpp&quot; file, which is included in the VC++ project, is compiled and put into a &quot;.obj&quot; file.)

Oh well, in any case, it works thanks to you JC.

Thanks,
-Dave
 
Well... My understanding is that <> is used for including headers that can be found in the paths known to system (i.e. in PATH environment variable). It has nothing to do with the compiled or not-compiled code.

When you build a library and then use that code in a different project, you would still include the header files for the library with &quot;.h&quot;... [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top