【发布时间】:2012-01-22 01:15:16
【问题描述】:
我有两个基类; “MemoryManagedObject”和“Gameobject”。这个想法是让我自己的类继承自“Gameobject”,后者继承自“MemoryManagedObject”。
#ifndef _GAME_OBJECT_H
#define _GAME_OBJECT_H
#include "MemoryManagedObject.h"
class GameObject : public MemoryManagedObject
{
public:
GameObject() { }
GameObject(bool UseMemPool): MemoryManagedObject(UseMemPool) { }
virtual ~GameObject() { }
long GetGameObjectID();
protected:
long mGameObjectID;
};
inline long GameObject::GetGameObjectID()
{
return mGameObjectID;
}
#endif
#ifndef _MEMORY_MANAGED_OBJECT_H
#define _MEMORY_MANAGED_OBJECT_H
#include <new>
class MemoryManagedObject
{
public:
MemoryManagedObject();
MemoryManagedObject(bool UseMemPool);
virtual ~MemoryManagedObject() { DecreaseRefCount(); }
bool IsAllocatedWithMemPool();
void* operator new(size_t size);
void* operator new(size_t size, bool UseMemPool);
void operator delete(void* obj);
long GetRefCount();
private:
void IncreaseRefCount();
void DecreaseRefCount();
long mRefCount;
bool mAllocatedWithMemPool;
};
#endif
但是我的编译器(android-ndk,gcc 4.4.1 afaik)在函数中向我抛出“未定义引用”链接器错误:~MemoryManagedObject 和 ~GameObject:未定义对 MemoryManagedObject::delete(void* obj) 和 MemoryManagedObject:: 的引用减少引用计数
这是为什么?我已经在编译中包含的 .cpp 文件中编写了所有方法;我认为我声明虚拟破坏的方式有问题,但我不知道为什么。
编辑:发布 testclass 和 cpp 文件:
TestClass1.h
#ifndef _TEST_CLASS_1_H
#define _TEST_CLASS_1_H
#include "GameObject.h"
class TestClass1 : public GameObject
{
public:
TestClass1();
TestClass1(bool UseMemPool);
~TestClass1();
void TestMe();
};
TestClass1.cpp
#include "TestClass1.h"
TestClass1::TestClass1()
{
}
TestClass1::TestClass1(bool UseMemPool): GameObject(UseMemPool)
{
}
TestClass1::~TestClass1()
{
}
void TestClass1::TestMe()
{
}
#endif
游戏对象.cpp
#include "GameObject.h"
GameObject::GameObject()
{
}
GameObject::GameObject(bool UseMemPool): MemoryManagedObject(UseMemPool)
{
}
GameObject::~GameObject()
{
MemoryManagedObject.cpp
#include "MemoryManagedObject.h"
#include "Engine.h"
#include <stdint.h>
MemoryManagedObject::MemoryManagedObject()
{
mAllocatedWithMemPool = false;
IncreaseRefCount();
}
MemoryManagedObject::MemoryManagedObject(bool UseMemPool)
{
mAllocatedWithMemPool = UseMemPool;
IncreaseRefCount();
}
MemoryManagedObject::~MemoryManagedObject()
{
DecreaseRefCount();
}
long MemoryManagedObject::GetRefCount()
{
return mRefCount;
}
void MemoryManagedObject::IncreaseRefCount()
{
mRefCount++;
}
void MemoryManagedObject::DecreaseRefCount()
{
mRefCount--;
if (mRefCount <= 0)
{
delete this;
}
}
bool MemoryManagedObject::IsAllocatedWithMemPool()
{
return mAllocatedWithMemPool;
}
void* MemoryManagedObject::operator new(size_t size)
{
Engine* engine = Engine::GetEngine();
void* alloc;
alloc = engine->GetMemoryManager()->Allocate(size);
return alloc;
}
void* MemoryManagedObject::operator new(size_t size, bool UseMemPool)
{
Engine* engine = Engine::GetEngine();
void* alloc;
alloc = engine->GetMemoryManager()->Allocate(size, UseMemPool);
return alloc;
}
void MemoryManagedObject::operator delete(void* obj)
{
Engine* engine = Engine::GetEngine();
MemoryManagedObject* memObj = (MemoryManagedObject*)obj;
engine->GetMemoryManager()->Deallocate(obj,memObj->IsAllocatedWithMemPool());
}
}
【问题讨论】:
-
声明没有错,但定义可能有问题。问题出在我们没有看到的代码中。
标签: android c++ inheritance linker android-ndk