Template class as static property problem

M

Michal M

Hi

I have been trying to write a class which could serve as a cache
manager for objects.

file Cache.h
-------------------
class CachedObject;

template<typename T>
class Cache {
public:
unsigned int cache_size;
unsigned int cache_counter;
CachedObject **objects_cache;
Cache() {
this->cache_size = 5;
objects_cache = new CachedObject*[this->cache_size];
};
CachedObject *GetInstance() {
//return new T();
if (this->cache_counter > 0) {
return objects_cache[--cache_counter];
} else {
return new T();
}
}
void AddCacheOrFree(CachedObject *o) { delete o; }
virtual ~Cache() {delete [] objects_cache;}
};

file CachedObject.h
----------------------------
#include "Cache.h"

class CachedObject {
public:
static Cache<CachedObject> myCache;
CachedObject() {};
void Destroy() {
CachedObject::myCache.AddCacheOrFree(this);
}
virtual ~CachedObject() {};
};


file main.cpp
------------------
#include "CachedObject.h"

int main(int argc, char* argv[]) {
CachedObject *myCachedObject = CachedObject::myCache.GetInstance();
myCachedObject->Destroy();
return 0;
}

It does not compile and gives linker error "main.o:main.cpp:(.text
+0xf): undefined reference to `CachedObject::myCache'".
If I uncomment //return new T(); in GetInstance it compiles without
errors.
I can't figure out what I am doing wrong. Could you help?

Oh another concern... If I had declared constructor as

Cache(int i) { ... .... }

how could I declare property

static Cache<CachedObject> myCache;

Thanks in advance for answer.
Regards
Michal Mierzwa
 
F

Fred

Hi

I have been trying to write a class which could serve as a cache
manager for objects.

file Cache.h
-------------------
class CachedObject;

template<typename T>
class Cache {
public:
        unsigned int cache_size;
        unsigned int cache_counter;
        CachedObject **objects_cache;
        Cache() {
                this->cache_size = 5;
                objects_cache = new CachedObject*[this->cache_size];
        };
        CachedObject *GetInstance() {
                //return new T();
                if (this->cache_counter > 0) {
                        return objects_cache[--cache_counter];
                } else {
                        return new T();
                }
        }
        void AddCacheOrFree(CachedObject *o) { delete o; }
        virtual ~Cache() {delete [] objects_cache;}

};

file CachedObject.h
----------------------------
#include "Cache.h"

class CachedObject {
public:
        static Cache<CachedObject> myCache;
        CachedObject() {};
        void Destroy() {
                CachedObject::myCache.AddCacheOrFree(this);
        }
        virtual ~CachedObject() {};

};

file main.cpp
------------------
#include "CachedObject.h"

int main(int argc, char* argv[]) {
        CachedObject *myCachedObject = CachedObject::myCache.GetInstance();
        myCachedObject->Destroy();
        return 0;

}

It does not compile and gives linker error "main.o:main.cpp:(.text
+0xf): undefined reference to `CachedObject::myCache'".
If I uncomment //return new T(); in GetInstance it compiles without
errors.
I can't figure out what I am doing wrong. Could you help?

Well, you have declared myCache as a static variable in CachedObject,
but have never defined it. In some .C file you need:
CachedObject::myCache;
 
M

Michal M

I have been trying to write a class which could serve as a cache
manager for objects.
file Cache.h
template<typename T>
class Cache {
public:
        unsigned int cache_size;
        unsigned int cache_counter;
        CachedObject **objects_cache;
        Cache() {
                this->cache_size = 5;
                objects_cache = new CachedObject*[this->cache_size];
        };
        CachedObject *GetInstance() {
                //return new T();
                if (this->cache_counter > 0) {
                        return objects_cache[--cache_counter];
                } else {
                        return new T();
                }
        }
        void AddCacheOrFree(CachedObject *o) { delete o; }
        virtual ~Cache() {delete [] objects_cache;}

file CachedObject.h
class CachedObject {
public:
        static Cache<CachedObject> myCache;
        CachedObject() {};
        void Destroy() {
                CachedObject::myCache.AddCacheOrFree(this);
        }
        virtual ~CachedObject() {};

file main.cpp
int main(int argc, char* argv[]) {
        CachedObject *myCachedObject = CachedObject::myCache.GetInstance();
        myCachedObject->Destroy();
        return 0;

It does not compile and gives linker error "main.o:main.cpp:(.text
+0xf): undefined reference to `CachedObject::myCache'".
If I uncomment //return new T(); in GetInstance it compiles without
errors.
I can't figure out what I am doing wrong. Could you help?

Well, you have declared myCache as a static variable in CachedObject,
but have never defined it. In some .C file you need:
  CachedObject::myCache;

Short, simple, accurate... Thank you!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top