Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
First of all, let me show you what have I done so far. I have 3 files.

MemoryM.h
Code:
#ifndef MEMORYM_H
#define MEMORYM_H


template<class T> T** allocate2DMemory(T **p, unsigned int xSize, unsigned int ySize);
template<class T> void dealloc2Dmemory(T **p, unsigned int xSize);
template<class T> T** realloc2Dmemory(T **p, unsigned int currentXSize, unsigned int newXSize, unsigned int newYSize);	

#endif

MemoryM.cpp
Code:
#include "MemoryM.h"

/*This will allocate memory for a 2D array. The array will be created outside the function, and will be
passed as a parameter inside this function.
*/
template<class T> 
T** allocate2DMemory(T **p, unsigned int xSize, unsigned int ySize){
	p = new T* [xSize];
	for (int i=0; i<xSize; i++) {
		p[i] = new T [ySize];
	}
	return p;
}	
/*This will deallocate the memory taken by a 2D dynamic arrays
It only needs the xSize of the array, and will delete every element
in each row of xSize*/
template<class T> 
void dealloc2Dmemory(T **p, unsigned int xSize){
	for (int i=0; i<xSize; i++) {
		delete p[i];
	}
	delete [] p;
}

/*This will delete the memory taken by a 2D array and will reallocate the
array with empty memory.*/
template<class T> 
T** realloc2Dmemory(char **p, unsigned int currentXSize, unsigned int newXSize, unsigned int newYSize){
	for (int i=0; i<currentXSize; i++) {
		delete p[i];
	}
	delete [] p;
	
	p = new T* [newXSize];
	for (int i=0; i<newXSize; i++) {
		p[i] = new T [newYSize];
	}
	return p;
}

And my main.cpp file
Code:
#include <iostream>
#include <cmath>
#include "MemoryM.h"

int main (int argc, char * const argv[]){
	int** t = allocate2DMemory<int>(t,100,100);
	
	return 0;
}

When I compile the program, it shows me this error:
Code:
Tool:0: collect2: ld returned 1 exit status
Tool:0: int** allocate2DMemory<int>(int**, unsigned int, unsigned int)
Tool:0: Undefined symbols:
The strange thing is that if I take the definitions and paste them into the main.cpp file, the program will compile fine. Why does this happen? I had wrote the same functions as normal functions (not as templates) and they were compiling just fine. When I converted them to templates they have definitions errors. Why is that?

I am using XCode 2.4
 

iSee

macrumors 68040
Oct 25, 2004
3,540
272
I don't know why the compiler is behaving differently depending on where you declare functions and whether or not they are template functions but I did see one thing that might be confusing the compiler:

At about line 6 of main.cpp you are both declaring t and passing it into a function. I don't know the C++ spec well enough to know if that is leagal, but it's the kind of thing that could confuse a compilter anyway.

Try declaring t first, and then using it, like this:
...
int** t;
t = allocate2DMemory<int>(t,100,100);
...
It may also make sense to either pass t into allocate2DMemory<>() or set it from the return value, but not both, although I don't understand enough about what you are doing to say for sure.
 

iSee

macrumors 68040
Oct 25, 2004
3,540
272
Doh!

AlmostThere is right! Geeze, I write template classes & functions all the time but I didn't spot the obvious!

The compiler needs access to the full function definition, not just the declaration in order to instantiate a template. When compiling your main, it needs to replace T with int and then compile that.

Sorry, didn't mean to give bad advice...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.