LNK2005函数指针数组“已定义”在.obj中

时间:2013-03-27 20:36:22

标签: c++ visual-studio linker-errors lnk2005

稍微介绍一下这个问题:我正在运行 Visual Studio 2012 中的 C ++ / DirectX11 中的渲染引擎(以32位模式编译) Windows 7 - 64位操作系统,我的Entity课程中出现了一个奇怪的链接错误( Entity3D 就像场景的基本角色) 。
我的所有边界体积类都继承自 Shape3D 类。每个实体都有一个 Shape3D * boundingVolume成员,在初始化时初始化为特定的形状类型。登记/> 当两个 Shape3D 之间发生碰撞时,我通过函数 - bool Intersect(Shape3D* a, Shape3D* b) 传递它们,然后函数检查它们的类型( Sphere / * Box * / Whatever)和类型表示一个数字,它是函数指针数组中函数的索引:

bool(*IntersectArray[4][4])(Shape3D* a, Shape3D* b) = 
{
    {
        IntersectSphereSphere,
        IntersectSphereBox,
        IntersectSphereOrientedBox,
        IntersectSphereFrustum,
    },
    {
        IntersectBoxSphere,
        IntersectBoxBox,
        IntersectBoxOrientedBox,
        IntersectBoxFrustum
    },
    {
        IntersectOrientedBoxSphere,
        IntersectOrientedBoxBox,
        IntersectOrientedBoxOrientedBox,
        IntersectOrientedBoxFrustum
    },
    {
        IntersectFrustumSphere,
        IntersectFrustumBox,
        IntersectFrustumOrientedBox,
        IntersectFrustumFrustum
    }
};

所以它就像一个虚拟调度。好的,InersectArray是函数的数组(在Intersect.h中声明),这就是给出链接错误的原因:

error LNK1169: one or more multiply defined symbols found
error LNK2005: "char (__cdecl*(* Engine::Collision::IntersectArray)[4])(class Engine::Collision::Shape3D *,class Engine::Collision::Shape3D *)" (?IntersectArray@Collision@Engine@@3PAY03P6ADPAVShape3D@12@0@ZA) already defined in Entity3D.obj
File Intersect.obj
Intersect.h 仅包含在 Entity3D.cpp 中,它不包含在 Entity3D.h 中,也不包含 Entity3D.h 包含的任何标题。 Entity3D.cpp 仅包含 Entity3D.h Intersect.h .I清理并重建,错误仍然存​​在。 Intersect(Shape3D a,Shape3D * b)*仅在 Entity3D中的 Entity3D 的一种方法中调用。 cpp 文件。项目中目前没有其他编译错误或警告。还有什么可能导致这样的问题?

1 个答案:

答案 0 :(得分:0)

解决了这个问题,我刚刚在 Intersect.cpp 文件中移动了 IntersectArray 的定义,因为现在这是唯一需要的地方。