Visual C++ 2005 Express Edition

Started by
10 comments, last by KittyRa 17 years, 12 months ago
I'm trying to compile a class template, but it seems like the compilater does not like these type of classes. Basically, when I compile the class, without using any template instructions, the compilation does work, buy using the template instructions, it does not. the compiler gives me the following errors:
Advertisement

------ Build started: Project: TemplateSample, Configuration: Debug Win32 ------
Compiling...
FileManagement.cpp
Linking...
TemplateSample.obj : error LNK2019: unresolved external symbol "public: __thiscall myFileManagement::FileManagement<int>::~FileManagement<int>(void)" (??1?$FileManagement@H@myFileManagement@@QAE@XZ) referenced in function _wmain
TemplateSample.obj : error LNK2019: unresolved external symbol "public: __thiscall myFileManagement::FileManagement<int>::FileManagement<int>(void)" (??0?$FileManagement@H@myFileManagement@@QAE@XZ) referenced in function _wmain
C:\Documents and Settings\Joly\Bureau\TemplateSample\Debug\TemplateSample.exe : fatal error LNK1120: 2 unresolved externals
Build log was saved at "file://c:\Documents and Settings\Joly\Bureau\TemplateSample\TemplateSample\Debug\BuildLog.htm"
TemplateSample - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
here is my class
[source langu="c++"]#pragma oncenamespace myFileManagement{	template <typename T>	class FileManagement	{	public:		FileManagement(void);		void Print(T);	public:		~FileManagement(void);	};}
[source langu="c++"]#include "StdAfx.h"#include "FileManagement.h"#include <iostream.h>namespace myFileManagement{	template <typename T>	FileManagement<T>::FileManagement(void)	{	}	template <typename T>	FileManagement<T>::~FileManagement(void)	{	}	template <typename T>	void FileManagement<T>::Print(T val )	{		cout<<val;	}}
here is an example of the using of the class:

// TemplateSample.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <cstdio>#include "FileManagement.h"int _tmain(int argc, _TCHAR* argv[]){	myFileManagement::FileManagement<int> p;	p.Print(1);	getchar();	return 0;}
Try not seperating your implementation from your definition. Everything should just go in the Header File.
Move the implementation into the header files. This is a problem specific to templates. So:


#pragma once

namespace myFileManagement
{
template
class FileManagement
{
public:
FileManagement(void);
void Print(T);
public:
~FileManagement(void);
};
}


becomes


#pragma once

namespace myFileManagement
{
template class FileManagement
{
public:
FileManagement(void) {}
~FileManagement(void) {}

void Print(T val)
{
cout <
Wow yeah, this board sucks with posting code, but that should give you the idea.
Quote:
the compiler gives me the following errors:

Uh? I see no errors. Oh, wait...
Quote:
------ Build started: Project: TemplateSample, Configuration: Debug Win32 ------
Compiling...
...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Yeah, there are some errors. But no question. Wait, there is some otehr infos:
Quote:
here is my class

?
There is no class here! You forgot to paste the code!
No, I'm wrong, it is in the next post again:
Quote:
(some code)

And in the next post
Quote:
(some code)

And in the next post too
Quote:
here is an example of the using of the class:
(some code)

...
Still, no question, but everyone is understanding what you really need: you want to get rid of your compiler errors. Hence the answers of Adam and the AP (both correct).

But it is not my point.

jolyqr, it would be very nice if you stop cutting what is really a single question into many posts (6 ?!?!). Or will you continue to use this uberstyley style, even if you need to cut up you posts in words and send a word at a time?

I have pretty some good news for you: we don't mind reading long posts, and the law don't enforce a regular use of the send button. Quite good, isn't it?

Regards,

This topic is closed to new replies.

Advertisement