template class to alloc memory

Discussion in 'C++' started by divya_rathore_@gmail.com, Dec 26, 2005.

  1. Guest

    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<float>(void)"
    (??1?$CADynamicAllocator@M@@UAE@XZ)

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

    error LNK2001: unresolved external symbol "public: __thiscall
    CADynamicAllocator<float>::CADynamicAllocator<float>(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, 2005
    #1
    1. Advertising

  2. 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<float>(void)"
    > (??1?$CADynamicAllocator@M@@UAE@XZ)
    >
    > error LNK2001: unresolved external symbol "public: float * * *
    > __thiscall CADynamicAllocator<float>::Allocate3DArray(float * *
    > *,int,int,int)"
    > (?Allocate3DArray@?$CADynamicAllocator@M@@QAEPAPAPAMPAPAPAMHHH@Z)
    >
    > error LNK2001: unresolved external symbol "public: __thiscall
    > CADynamicAllocator<float>::CADynamicAllocator<float>(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
    Victor Bazarov, Dec 26, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob
    Replies:
    1
    Views:
    605
    .NET Follower
    Feb 13, 2004
  2. B. Gandhi

    Memory Alloc/Dealloc query

    B. Gandhi, Sep 23, 2003, in forum: C Programming
    Replies:
    6
    Views:
    596
    Peter Shaggy Haywood
    Sep 25, 2003
  3. Vulcan Fire

    string vs const int memory alloc

    Vulcan Fire, Apr 12, 2005, in forum: C Programming
    Replies:
    6
    Views:
    630
    Vulcan Fire
    Apr 19, 2005
  4. Ryan Wang

    Memory alloc for a Function

    Ryan Wang, Nov 14, 2005, in forum: C Programming
    Replies:
    10
    Views:
    597
    Dik T. Winter
    Nov 15, 2005
  5. Angus
    Replies:
    29
    Views:
    845
    Nick Keighley
    Jul 29, 2010
Loading...

Share This Page