Allocating dynamic memory with a class template.

Started by
6 comments, last by ApochPiQ 13 years, 6 months ago
I'm kinda new to CPP and trying to get the hang of it. I've been working with C at school and im using java at a regular basis so im not that new to programming.

I'm trying to learn different technics for C++ and one of them is using Templates with dynamic memory.

Only problem is that i keep getting a "undefined reference to" error;


My code is pretty simple. I've got a class named Array using a templat that holds a un initialized array and a size variable, it also has a constructor, a copy constructor, destructor and overrides the [] operator.
No need to post the implementation of the functions.


template <class T>class Array {public:	int size;private:	T *arrayPtr[];public:	Array(int);	Array(const Array &);	virtual ~Array();	T &operator[](int);};




The main function is straight forward. I create a pointer and try to allocate memory to it.

#include <iostream>#include "Array.h"#include "Object.h"using namespace std;int main(){//creating a pointer and allocating memory for a new array of size 5 and type Object	Array<Object> *array1 = new Array<Object>(5);//creating a pointer and allocating memory for a copy of array1.	Array<Object> *array2 = new Array<Object>(array1);//Some other code that's not important.	delete array1;	delete array2;	return 0;}



Problem here is that on both pointers i get the following message "...../src/Main.cpp:18: undefined reference to `Array<Object>::Array(int)'"

Any suggestions?
Idono! That's me!
Advertisement
Read through these FAQs - then it should be clear what's going on [smile]

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Quote:Original post by ApochPiQ
Read through these FAQs - then it should be clear what's going on [smile]


Sweet! Thx. Been looking forever for an something that explains it. My c+ Book only covers so much.

But with this other problems to solve have arrised. I dont really want to use inline functions.
Idono! That's me!
For compilers nowadays inline is at best a suggestion. I'm not completely sure on it, but modern MSVCs completely ignore it for actual inlining purposes and just do what they thinks would be best for the given size vs speed focus.
Quote:Original post by BitMaster
For compilers nowadays inline is at best a suggestion. I'm not completely sure on it, but modern MSVCs completely ignore it for actual inlining purposes and just do what they thinks would be best for the given size vs speed focus.


So what your saying is that if i dont specifically make a funciton inline in the header file, the compiler can decide wich is best?
Idono! That's me!
Quote:Original post by Idono87
So what your saying is that if i dont specifically make a funciton inline in the header file, the compiler can decide wich is best?


The opposite. If you /do/ make the function inline, the compiler will decide. The inline keyword is only a hint.

Actually, some linkers can do inlining now too, if you compile the objects in a particular way, regardless of whether or not the inline keyword is used.

Also, in C++ it's somewhat unusual to dynamically allocate an Array like you've done. Typically you'd use automatic storage:

int main(){    Array<Object> array1(5);    Array<Object> array2(array1);    return 0;    // array1 and array2 are destroyed automatically.}


A bit of general advice: though Java and C++ look similar, the similarities don't really run much deeper than that. So try to avoid importing the techniques you use in Java directly in to C++ and treat C++ as an entirely new language.
Quote:Original post by the_edd
Also, in C++ it's somewhat unusual to dynamically allocate an Array like you've done. Typically you'd use automatic storage:


K. My book didnt cover to much about dynamic memory, i'll read in to that.
The reason i used the new keyword like that is that i got the impression that it's the only way to call the constructor.
Idono! That's me!
Nope; you can construct values on the stack as well, using the syntax the_edd showed. In C++, when you create an object, exactly one constructor will always be called. Oftentimes this is the default constructor, but not necessarily.

These FAQs as well as this section are probably good follow-up reading for you. In fact, if you can spare a couple of hours, you should probably just read the entire site - it's full of great information on how C++ works and how to make effective use of the language.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement