PDA

View Full Version : C++ Help (templates) Novice



Luke1_1
10-26-2001, 10:46 AM
I am a C++ novice and I can't figure out why i am having problem trying to compile my program. I have a small program. I AM RUNNING MICROSOFT VISUAL C++ 6.0. I will appreciate any help you guys can give!!

Here is the error I keep getting --->

ass121.obj : error LNK2001: unresolved external symbol "public: __thiscall bubble<int>::bubble<int>(int)" (??0?$bubble@H@@QAE@H@Z)

Debug/bubble.exe : fatal error LNK1120: 1 unresolved externals

Here is the Main program:
---------------------------------------

#include "iostream.h"
#include "bb.h"

int main()
{
bubble <int> can(5);
return 0;
}

--------------------------------------

Here is the Header file:

#ifndef TEST_H
#define TEST_H

template <class T>
class test {
public:
test(int = 1);
void print();
private:
T xx;
};

#endif

-------------------------------------------

And Here is the function file:

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

template< class x>
bubble<x>::bubble(int howmany)
{
howmany = (howmany > 0 && howmany < 15) ? howmany : 1;

HowMany = howmany;
numbers = new int[howmany];
for (int ww = 0; ww <= HowMany-1; ++ww)
numbers[ww] = 0;

getnumbers();
}

template<class x>
void bubble<x>::getnumbers()
{
for (int qq1 = 0; qq1 <= HowMany-1; ++qq1)
{
cout << endl;
cout << "Enter value: " << qq1 << " ";
cin >> numbers[qq1];
}
sort();
}


template<class x>
void bubble<x>::sort()
{
int temp;
for (int ss = 0; ss <= (HowMany - 2); ++ss)
{
for (int ww = 0; ww <= (HowMany -2); ++ww)
{
if (numbers[ww] >= numbers[ww + 1] )
{
temp = numbers[ww];
numbers[ww] = numbers[ww + 1];
numbers[ww + 1] = temp;
}
}
}
print();
}

template<class x>
void bubble<x>: http://www.opengl.org/discussion_boards/ubb/tongue.gifrint()
{
for (int aa = 0; aa <= HowMany-1; ++aa)
{
cout << endl;
cout << numbers[aa];
}
}

Korval
10-26-2001, 11:51 AM
What precisely does this have to do with OpenGL?

jabe
10-26-2001, 12:03 PM
sorry when replying OT, but to answer your question:
the body of every template class method has to be implemented in your header. you can't place template class code in object files (.o, .obj), because the class code will generated while compile time and cannot preprocessed into object files.
so put all your code from you function file into the header file and everything will work fine.

regards,
jan

Luke1_1
10-26-2001, 02:36 PM
wow...thanks alot jabe...I was stuck on that problem for 2 days...I recompiled it..and it worked fine...

thanx again!!