Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code with Templates not Linking 1

Status
Not open for further replies.

netprik1

Programmer
Jul 19, 2007
7
US
I'm having a problem getting templates to work in a program. I haven't used them for a while, so maybe I'm confused somehow as to how to use them correctly. I looked it up and it seems that I have the right syntax though. I do not get a compile error. I get a linker error. I'm using Deviant C++ by the way, but I don't think this is an issue specific to the compiler. Here's the code that's giving me problems...

In a file called arrays.cpp, I have this:

Code:
#include "arrays.h"

template <typename T>  GenericArray2DStruct<T>* allocate2DArray (unsigned int xs, unsigned int ys) {
    int i;
    //allocate the struct itself
    GenericArray2DStruct <T> *prod =  (GenericArray2DStruct <T>*) malloc(sizeof (GenericArray2DStruct <T>));
    if (prod == NULL) {
        printf ("Insufficient memory to execute!");  }
    //allocate the array of pointers to arrays
    prod->data =(T**) malloc(sizeof(T*)*ys);
    if (prod->data == NULL) {
        printf ("Insufficient memory to execute!");  }
    //allocate each of the arrays
    for (i=0;i<ys;i++) {
        prod->data [i] = (T*) malloc(sizeof(T)*xs);
        if (prod->data [i] == NULL) {
        printf ("Insufficient memory to execute!");  }}
    prod->xSize = xs;
    prod->ySize = ys;
    return prod;    }

The arrays.h header has this:

Code:
template <class T>
typedef struct GenericArray2DStruct{
	int xSize;
	int ySize;
	T **data;	};

template <class T>
GenericArray2DStruct<T>* allocate2DArray (unsigned int xs, unsigned int ys);

Now the code that gives me problems looks like this:

Code:
#include "arrays.h"

using namespace std;

image1Array = allocate2DArray <unsigned char> (100, 100);

The linker gives me an error saying:
[Linker error] undefined reference to `GenericArray2DStruct<unsigned char>* allocate2DArray<unsigned char>(unsigned int, unsigned int)'

Can anyone help me out? Why is this happening and how do I fix it? Thanks in advance!
 
Why are you using malloc() in a C++ program?
It should be [tt]new[/tt] and [tt]delete[/tt] all the way.

AFAIK, the whole of the template code should be in the header file, so the compiler can create the appropriate instance whenever you reference it.



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
That doesn't fix anything though...I've even tried shoving all the code into a single file, but that still doesn't fix anything. It looks like it doesn't understand that this:

Code:
allocate2DArray <unsigned char> (100, 100);

is trying to access this:

Code:
template <class T>
GenericArray2DStruct<T>* allocate2DArray (unsigned int xs, unsigned int ys);

It doesn't seem to see that these are one and the same, and that the first is a reference to the second.

And as far as using malloc goes, thanks a lot for the suggestion! I was converting this code to C++ from C (the templates are new additions), and I forgot that C++ doesn't typically use malloc anymore. Still not what's stopping my code from working, though.
 
Alright, so the problem's solved. Apparently, the fact that all template declarations needed to be in the header file was the only problem. I didn't REBUILD the code, I only BUILT it, and that's why my changes had no effect at first. I rebuilt the code, and it worked. Now the other problem I'm now having is my code just got slower by a factor of 2 - anyone know why?
 
Slower compared to what?
You need to describe (or show us) how version 1 was implemented as well.

Changing
[tt]unsigned char image1array[100][100];[/tt]
to
[tt]image1Array = allocate2DArray <unsigned char> (100, 100);[/tt]
for example?

Or just making [tt]allocate2DArray()[/tt] a template function?

Or did you just time the debug version where you previously timed the release version?



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Ah, yes, I see...that's what happened. I'm traditionally a Java developer, so I forget about things like that in C++. I ran the code in debug mode the second time. No problem now - the code's just as fast as it was before. Thanks for all the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top