Visual C++ Question

KarlHungus

Senior member
Nov 16, 1999
638
0
0
Ok, first off I'm in the process of learning Visual C++ after working on Unix systems for the past 4 years. The problem I'm having is as follows: my template array class refuses to compile. I'm suspecting that it has something to do with not generating the proper object files (though it could simply be programmer error). Here's a small snippet:

template <class ArrayType> class Array1
{
protected:
Array1(){}
int x;
ArrayType *array;
public:
Array1(const int &amp;i);
~Array1(){if(array) delete [] array;}
void set(const int &amp;i, const ArrayType &amp;a){array[ i ] = a;}
ArrayType get(const int &amp;i){return array[ i ];}
};

template <class ArrayType> Array1<ArrayType>::Array1(const int &amp;i)
{
x = i;
array = new ArrayType[x];
}

Everything compiles just fine when I stick everything in the header file, but when I move the constuctor (destructor/functions) over to a cpp file I get the following error(s):

hello.obj : error LNK2001: unresolved external symbol &quot;public: __thiscall Array1<float>::Array1<float>(int const &amp;)&quot; (??0?$Array1@M@@QAE@ABH@Z)
Debug/hello.exe : fatal error LNK1120: 1 unresolved externals

Any help would be appreciated.

edit - just realized [ i ] makes the text go italic with no spaces...
 

shoryuken

Junior Member
Oct 30, 2000
12
0
0
Try rebuild-all first, and then compile the code. That worked for me whenever I had linking errors.
 

Engine

Senior member
Oct 11, 1999
519
0
0
This may be too simple, but did you #include the .h file in your .cpp file?
 

KarlHungus

Senior member
Nov 16, 1999
638
0
0
Yep, the .h file is linked. And I should mention that the class works perfectly when using a standard class type such as float.
 

Engine

Senior member
Oct 11, 1999
519
0
0
So, it bombs when you try to declare an array of a user-defined class? Hrm.
Linking errors always made my brain hurt :confused:
 

KarlHungus

Senior member
Nov 16, 1999
638
0
0
Not exactly. It only bombs when I move the template array functions into the cpp file. I can create functions of static data types without the errors (such as an array of floats).
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
Are you using precompiled headers? If you are, you should also include the precompiled header in your .CPP file, and if not, perhaps you might want to try using a precompiled header and see if the problem goes away?


:)atwl
 

KarlHungus

Senior member
Nov 16, 1999
638
0
0
I'm assuming the header is pre-compiled, but as I said I'm just getting used to Visual C++. The file names are arrays.h and arrays.cpp, there is a corresponding arrays.obj file which I'm assuming (again) is the equivalent of a Unix .o file.
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
Templates are known to cause problems when you put them in .cpp file. I remember having the same problem under gcc as well, so it isn't VC++ bug. What you can do, is move all functions to .template file, and then do #include &quot;*.template&quot; at the end of .h file.

 

KarlHungus

Senior member
Nov 16, 1999
638
0
0
Thanks for the help. I think I'll just leave the function definitions in the header file. It removes the errors, so I figure the kludge can't be all that bad...