使用多个基类时链接错误

时间:2012-01-22 01:15:17

标签: android c++ inheritance linker android-ndk

我有两个基础班; “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)在函数中抛出了“Undefined Reference”链接器错误:~MemoryManagedObject和~GameObject:对MemoryManagedObject :: delete(void * obj)和MemoryManagedObject ::的未定义引用DecreaseRefCount

这是为什么?我已经编写了包含在编译中的.cpp文件中的所有方法;我相信我如何声明虚拟析构函数有问题,但我不知道为什么。

编辑:发布测试类和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

GameObject.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());
}

}

1 个答案:

答案 0 :(得分:1)

您正在编写内联虚拟析构函数,这通常会导致它们在每个编译单元中作为弱符号发出。另外,由于类中的第一个(也是唯一的)虚函数是析构函数,因此它将是vtable将发出的键函数。

我猜这两件事情相互影响很严重,导致内联析构函数不包含在最终链接中。

在任何情况下,解决方案都是将虚拟析构函数的定义移到cpp文件中。除非您在堆栈上声明对象的实例,否则除非通过vtable,否则将永远不会调用析构函数。