Click here to Skip to main content
15,922,007 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow to keep my server alive Pin
Bill Wilson21-Jun-02 8:52
Bill Wilson21-Jun-02 8:52 
AnswerRe: How to keep my server alive Pin
Ramu Pulipati21-Jun-02 10:18
Ramu Pulipati21-Jun-02 10:18 
AnswerRe: How to keep my server alive Pin
A.A.21-Jun-02 10:26
A.A.21-Jun-02 10:26 
GeneralRe: How to keep my server alive Pin
Bill Wilson24-Jun-02 6:20
Bill Wilson24-Jun-02 6:20 
GeneralMultimedia netwok Pin
21-Jun-02 8:32
suss21-Jun-02 8:32 
GeneralRe: Multimedia netwok Pin
dazinith21-Jun-02 8:36
dazinith21-Jun-02 8:36 
GeneralRe: Multimedia netwok Pin
Anonymous10-Oct-02 6:06
Anonymous10-Oct-02 6:06 
GeneralTemplate Troubles (Solved) Pin
Raskolnikov21-Jun-02 8:32
Raskolnikov21-Jun-02 8:32 
I am using a template class and everyithing works fine until I try to break my code into multiple files. Here is a minimal test case that demonstrates my problem. I am using Visual C++ 6 SP 5 Window XP.

******Solved*************
Visual C++ 6 requires that the implementation and declaration of a template reside in the same file. I can either put everything in the .cpp file or the .h but it has to be in one file.


//Onefile.cpp
#include <vector>
#include <iostream>

using namespace std;

template
class Test
{
public:
// constructors

Test(int size);
~Test() {};

// opertors
T & operator[](int offset) { return itsVector[offset];}
Show();


vector itsVector;
int itsLength;
};

template
Test::Test(int size)
: itsLength(size), itsVector(size)
{
for (int i = 1; i itsVector = i;
}
template
Test::Show()
{
for (int i = 1; i < itsLength; i++)
cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (','));
}

int main()
{
Test v(4);
v.Show();
v[1] = 9;
v.Show();
return 0;
}

This compiles links and executes just fine. Here is the problem.

//Test.h
#include <vector>
#include <iostream>

using namespace std;

template
class Test
{
public:
// constructors

Test(int size);
~Test() {};

// opertors
T & operator[](int offset) { return itsVector[offset];}
Show();


vector itsVector;
int itsLength;
};

//test.cpp
#include "Test.h"

template
Test::Test(int size)
: itsLength(size), itsVector(size)
{
for (int i = 1; i itsVector[i] = i;
}
template
Test::Show()
{
for (int i = 1; i < itsLength; i++)
cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (','));
}

//main.cpp
#include <iostream>
#include "Test.h"

using namespace std;

int main()
{
Test v(4);
v.Show();
v[1] = 9;
v.Show();
return 0;
}

When I try to build I get the following linking error.

main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Test::Show(void)" (?Show@?$Test@H@@QAEHXZ)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall Test::Test(int)" (??0?$Test@H@@QAE@H@Z)
Debug/TestTemplate.exe : fatal error LNK1120: 2 unresolved externals

All the errors disapear if quit using templates.

Does anyone know what I am doing wrong?

GeneralRe: Template Troubles Pin
sultan_of_6string21-Jun-02 9:04
sultan_of_6string21-Jun-02 9:04 
GeneralRe: Template Troubles Pin
redeemer21-Jun-02 12:57
redeemer21-Jun-02 12:57 
GeneralRe: Template Troubles Pin
sultan_of_6string21-Jun-02 15:51
sultan_of_6string21-Jun-02 15:51 
GeneralRe: Template Troubles Pin
jbarton21-Jun-02 10:36
jbarton21-Jun-02 10:36 
GeneralRe: Template Troubles (Solved) Pin
Raskolnikov21-Jun-02 13:27
Raskolnikov21-Jun-02 13:27 
GeneralRe: Template Troubles (Solved) Pin
Paul M Watt21-Jun-02 18:30
mentorPaul M Watt21-Jun-02 18:30 
GeneralRe: Template Troubles (Solved) Pin
Tim Smith22-Jun-02 4:56
Tim Smith22-Jun-02 4:56 
GeneralRe: Template Troubles (Solved) Pin
Raskolnikov22-Jun-02 10:09
Raskolnikov22-Jun-02 10:09 
Generalimage copying problem Pin
SilentWarrior21-Jun-02 8:12
SilentWarrior21-Jun-02 8:12 
GeneralRe: image copying problem Pin
Speedy21-Jun-02 11:33
Speedy21-Jun-02 11:33 
GeneralConfused about Bound sockets and deamons Pin
Jason Hihn21-Jun-02 7:51
Jason Hihn21-Jun-02 7:51 
GeneralRe: Confused about Bound sockets and deamons Pin
21-Jun-02 10:28
suss21-Jun-02 10:28 
GeneralRe: Confused about Bound sockets and deamons Pin
jferrell121123-Jun-02 3:37
jferrell121123-Jun-02 3:37 
GeneralSend Message WM_CLOSE Pin
MyEden21-Jun-02 7:16
MyEden21-Jun-02 7:16 
GeneralRe: Send Message WM_CLOSE Pin
21-Jun-02 9:34
suss21-Jun-02 9:34 
GeneralRe: Send Message WM_CLOSE Pin
MyEden21-Jun-02 22:01
MyEden21-Jun-02 22:01 
GeneralMySql++ Pin
klawipo21-Jun-02 6:37
klawipo21-Jun-02 6:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.