Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

c++ - "undefined reference" to a template class function

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...