2

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. Looking for compilation ready with minimal changes.

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

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

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;

int 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;
}
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
Alok
  • 73
  • 1
  • 6

4 Answers4

11

You can't put template declarations in .cpp files like that. Template declarations and implementation need to be visible in the same translation unit. Put template implementations in headers that you #include directly.

Maister
  • 4,978
  • 1
  • 31
  • 34
  • Maister, the same code is compilable in Gcc 3.1.2 but not in 4.1.1. Where can I find that what change they have made in compiler so that it is not accepting the in GCC 4.1.1 – Alok Dec 06 '10 at 22:05
7

Define your function templates in the header. Compiler needs to see them.

Cheers & hth.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
3

Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.

From http://www.cplusplus.com/doc/tutorial/templates/

Jack
  • 10,943
  • 13
  • 50
  • 65
Gang Shen
  • 39
  • 1
  • 1
    **-1** "the implementation (definition) of a template class or function must be in the same file as its declaration.", that is incorrect. then "That means that we cannot separate the interface in a separate header file" is again incorrect. only the final "we must include both interface and implementation in any file that uses the templates." is (in practice) correct, although it's possible to specialize templates for a limited set of types and then have a private implementation. – Cheers and hth. - Alf Aug 08 '14 at 18:50
0

For anyone passing by

you can also #include the implementation files in main

in main:

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

#include "arrayobjclass.cpp"
#include "arrayclass.cpp"
Eric Aya
  • 69,473
  • 35
  • 181
  • 253