473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

template class to alloc memory

I have a problem regarding template classes. I am trying to write a
template class which allocates dynamic memory for 2D/3D arrays.
Consider a template class to allocate a 2D array:

//DynAlloc.h
template <class T>
class CADynamicAllocator
{

public:
CADynamicAllocator();
virtual ~CADynamicAllocator();
public:
T** Allocate2DArray(T** array, int width, int height);
DeAllocate2DArray(T** array, int width, int height);
};

template <class T>
T** CADynamicAllocator<T>::Allocate2DArray(T** array, int width, int
height)
{
// No. of rows = 'height'
// No. of cols = 'width'
int y;
//Firstly, allocate height.
array = new T*[height];
//And then the width..
for (y=0; y<height; y++) array[y] = new T[width];

return array;
}
And now consider the usage in main.cpp

//main.cpp
#include "DynAlloc.h"
int main()
{
float*** array3D = NULL;
int x,y,z;
x=y=z=10;
CADynamicAllocator<float> dynAlloc;
m_Stack = dynAlloc.Allocate3DArray(m_Stack,x,y,z);

}

Now the Error message that the compiler (vc++ 6.0) is giving:

unresolved external symbol "public: virtual __thiscall
CADynamicAllocator<float>::~CADynamicAllocator<flo at>(void)"
(??1?$CADynamicAllocator@M@@UAE@XZ)

error LNK2001: unresolved external symbol "public: float * * *
__thiscall CADynamicAllocator<float>::Allocate3DArray(float * *
*,int,int,int)"
(?Allocate3DArray@?$CADynamicAllocator@M@@QAEPAPAP AMPAPAPAMHHH@Z)

error LNK2001: unresolved external symbol "public: __thiscall
CADynamicAllocator<float>::CADynamicAllocator<floa t>(void)"
(??0?$CADynamicAllocator@M@@QAE@XZ)

What are the syntax (among any other) issues that I am missing here?
Any adivce would be really appreciated.

regards,
D. Rathore

Dec 26 '05 #1
1 2039
di************@gmail.com wrote:
I have a problem regarding template classes. I am trying to write a
template class which allocates dynamic memory for 2D/3D arrays.
Consider a template class to allocate a 2D array:

//DynAlloc.h
template <class T>
class CADynamicAllocator
{

public:
CADynamicAllocator();
You declared this member (the default constructor) but you haven't
defined it. Where is its body?
virtual ~CADynamicAllocator();
You declared this member (the destructor) but you haven't defined it.
Where is its body?
public:
T** Allocate2DArray(T** array, int width, int height);
DeAllocate2DArray(T** array, int width, int height);
};

template <class T>
T** CADynamicAllocator<T>::Allocate2DArray(T** array, int width, int
height)
{
// No. of rows = 'height'
// No. of cols = 'width'
int y;
//Firstly, allocate height.
array = new T*[height];
//And then the width..
for (y=0; y<height; y++) array[y] = new T[width];

return array;
}
And now consider the usage in main.cpp

//main.cpp
#include "DynAlloc.h"
int main()
{
[...]
CADynamicAllocator<float> dynAlloc;
You create an object of this type. The compiler needs a way to
construct the object. You declared the default constructor. The
compiler sees its declaration and creates the code to invoke it.
The compiler aldo needs a destructor to clean up after 'main' exits.
Since you declared the destructor, the compiler creates code to
invoke it as well.
[...]
}

Now the Error message that the compiler (vc++ 6.0) is giving:

unresolved external symbol "public: virtual __thiscall
CADynamicAllocator<float>::~CADynamicAllocator<flo at>(void)"
(??1?$CADynamicAllocator@M@@UAE@XZ)

error LNK2001: unresolved external symbol "public: float * * *
__thiscall CADynamicAllocator<float>::Allocate3DArray(float * *
*,int,int,int)"
(?Allocate3DArray@?$CADynamicAllocator@M@@QAEPAPAP AMPAPAPAMHHH@Z)

error LNK2001: unresolved external symbol "public: __thiscall
CADynamicAllocator<float>::CADynamicAllocator<floa t>(void)"
(??0?$CADynamicAllocator@M@@QAE@XZ)

What are the syntax (among any other) issues that I am missing here?


You're missing the fact that both the default constructor and the
destructor need to be implemented.

V
Dec 26 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Greg Buchholz | last post by:
/* I've been experimenting with some generic/polytypic programs, and I've stumbled on to a problem that I can't quite figure out. In the program below, I'm trying to define a generic version of...
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
8
by: Jess | last post by:
Hi, I have a template function that triggered some compiler error. The abridged version of the class and function is: #include<memory> using namespace std; template <class T>
4
by: abir | last post by:
I am matching a template, and specializing based of a template, rather than a single class. The codes are like, template<template<typename T,typename Alloc = std::allocator<T> class pix{ }; ...
4
by: * Tong * | last post by:
First of all, thanks mlimber for answering my previous question. Now... I'm following the example in "C++ Templates: The Complete Guide", section 5.4 Template Template Parameters, and I'm...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.