LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   "undefined reference" to a template class function (https://www.linuxquestions.org/questions/programming-9/undefined-reference-to-a-template-class-function-356981/)

btb 08-25-2005 04:08 PM

"undefined reference" to a template class function
 
Hi all, I am writing a template class for an array of objects, call it arrayobjclass, which holds pointers to other objects, specifically to other arrays in my implementation. The arrays are implemented as objects as well, call them arrayclass.

when I try to test my classes with the following line,

g++ main.cpp arrayclass.cpp arrayobjclass.cpp -o arrayobj.exe

I get the following error:

/tmp/ccEpROXj.o(.text+0x17c): In function `main':
: undefined reference to `arrayobjclass<arrayclass, int>::arrayobjclass(int)'
/tmp/ccEpROXj.o(.text+0x1dc): In function `main':
: undefined reference to `arrayobjclass<arrayclass, int>::addelem(arrayclass*)'
collect2: ld returned 1 exit status

I really can't understand what is wrong. any help would be appreciated. the short relevant part of the code is below if it helps. THANKS IN ADVANCE!

This is what i have in main:
--------------------------------------------------------
#include "arrayclass.h"
#include "arrayobjclass.h"
#include <iostream>

// 5 arrays of 10 maxsize each
#define MAXSIZE_array 10
#define NUMB_objs 5

using namespace std;

main () {

//create a simple array as an arrayclass object
arrayclass * numbers1 = new arrayclass (MAXSIZE_array);

//array of objects to hold pointers to simple arrays as created above
arrayobjclass<arrayclass,int> * myobjs = new arrayobjclass<arrayclass,int> (NUMB_objs);

//fill up the simple array
int i;
for (i=0; i<10; i++) {
numbers1->addelem(i); }

//add a pointer to the simple array in my array of objects
myobjs->addelem(numbers1);

}
--------------------------------------------------------
//arrayobjclass.h
//declarations of an array of pointers to objects

template <class obj, class key>
class arrayobjclass {

private:
//obj * arrayptr;
obj * objarray [];
int maxsize;
int totalelem;
public:
arrayobjclass(int);
bool addelem(obj *);
};

--------------------------------------------------------
//arrayobjclass.cpp
//implementation of arrayobjclass, array of pointers to objects

#include "arrayobjclass.h"
#include "arrayclass.h"

template <class obj,class key>
arrayobjclass<obj,key>::arrayobjclass (int size){

maxsize=size;
objarray = new obj[maxsize];
totalelem = 0;
}

template <class obj, class key>
bool arrayobjclass<obj,key>::addelem (obj * newobj) {

if (totalelem < maxsize ) {
objarray[totalelem] = newobj;
totalelem ++;
return true;
}
return false;
}

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

//arrayclass.h

class arrayclass {

private:
int * arrayptr;
int maxsize;
int totalelem;
public:
arrayclass(int);
bool addelem(int);
};

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

//arrayclass.cpp

#include "arrayclass.h"

arrayclass::arrayclass (int size){

maxsize=size;
arrayptr = new int[maxsize];
totalelem = 0;
}

bool arrayclass::addelem (int addval) {

if (totalelem < maxsize ) {
arrayptr[totalelem] = addval;
totalelem ++;
return true;
}
return false;
}

Mara 08-25-2005 04:41 PM

G++ requires template header in the same file as implementation. In practice it means implementation inside .h file.

deiussum 08-25-2005 04:43 PM

When working with templates, you need to have the full definition in the header. There's a thread in here somewhere that discusses this more. Search for that if you want more details on why that is.

Edit: Here's the thread I mentioned above.

btb 08-25-2005 05:02 PM

Thanks guys! problem's not solved, but atleast now I know what the problem is to look into. Thanks :)


All times are GMT -5. The time now is 02:13 AM.