LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-25-2005, 04:08 PM   #1
btb
LQ Newbie
 
Registered: Jun 2005
Posts: 20

Rep: Reputation: 0
"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;
}
 
Old 08-25-2005, 04:41 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
G++ requires template header in the same file as implementation. In practice it means implementation inside .h file.
 
Old 08-25-2005, 04:43 PM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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.

Last edited by deiussum; 08-25-2005 at 04:49 PM.
 
Old 08-25-2005, 05:02 PM   #4
btb
LQ Newbie
 
Registered: Jun 2005
Posts: 20

Original Poster
Rep: Reputation: 0
Thanks guys! problem's not solved, but atleast now I know what the problem is to look into. Thanks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"undefined reference"linker error for static field w/ C++ astorm Programming 5 08-27-2008 03:00 AM
Undefined Screen " XFree86" reference by serverLayout "XFree86 Configured" comox *BSD 7 01-17-2005 05:47 PM
When "function pointer" meets "template"... I can't get rid of this compiling er cyu021 Linux - Software 3 12-17-2004 07:52 PM
Kernel make bzImage error "undefined reference" w/ Prism2.5 USB driver under 2.6 joachimvb Mandriva 1 09-24-2004 07:43 PM
ncurses compile error: "undefined reference" Mr. Eek Linux From Scratch 1 05-12-2003 04:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration