我正在开发一个项目,我正在使用外部库IrrLicht。我遇到的问题是我包含的外部头文件也有定义。链接器不喜欢这个,它抱怨重新定义。我应该去另一个图书馆还是这种方式来解决这个问题?哦顺便说一句,我正在和2010年合作。
示例math3.h
#ifndef MATH_H
#define MATH_H
#include "quat.h"
class Point{
public:
float x;
float y;
float z;
Point (float _x, float _y, float _z){
x=_x;
y=_y;
z=_z;
}
Point(){
x=0;
y=0;
z=0;
}
irr::core::vector3df operator-(Point p){
return irr::core::vector3df(this->x-p.x,this->y-p.y,this->z-p.z);
}
float distance(Point p){
return sqrt((this->x-p.x)*(this->x-p.x)+(this->y-p.y)*(this->y-p.y)+(this->z-p.z)*(this->z-p.z));
}
Point operator+(irr::core::vector3df v){
return Point(this->x+v.X,this->y+v.Y,this->z+v.Z);
}
};
collidables.h
#ifndef COLLIDABLES_H
#define COLLIDABLES_H
#include "quat.h"
#include "math3.h"
class Sphere{
public:
Point cm;
float R;
irr::core::vector3df speed;
};
quat.h
#ifndef QUAT_H
#define QUAT_H
#include "irrlicht.h"
#endif
InitializeItems.cpp
#include <vector>
#include <time.h>
#include "InitializeItems.h"
#include "math3.h"
#include "quat.h"
#include "collidables.h"
#define SQRT2 1.4142135;
#define DIVSQRT2 0.70710678118;
std::vector<Sphere> InitializeAtoms(int N, float R)
{
std::vector<Sphere> spheres;
spheres.reserve(N);
srand(time(NULL));
Sphere sph;
for (int i=0;i<N;i++){
sph.cm=Point(rand()%2*N*R,rand()%2*N*R,rand()%2*N*R);
sph.R=R;
sph.speed=irr::core::vector3df(rand()%10,rand()%10,rand()%10);
spheres.push_back(sph);
}
return spheres;
}
std::vector<Molecule3> InitializeMol (int N, float R){
std::vector<Molecule3> mols;
mols.reserve(N);
srand(time(NULL));
Molecule3 m;
for (int i=0;i<N;i++){
m.cm=Point(rand()%2*N*R,rand()%2*N*R,rand()%2*N*R);
m.R=R;
m.sph1=m.sph2=m.sph3=(m.cm-Point(0,0,0));
m.orientation=irr::core::quaternion(rand(),rand(),rand(),rand());
m.orientation.normalize();
m.w=AxisAngle(irr::core::vector3df(rand(),rand(),rand()),rand()%360);
m.w.v.normalize();
irr::core::vector3df temp1,temp2,temp3;
temp1=irr::core::vector3df(-R,-R,0);
temp2=irr::core::vector3df(R,-R,0);
temp3=irr::core::vector3df(0,R,0);
temp1=fromRotation(m.orientation,temp1);
temp2=fromRotation(m.orientation,temp2);
temp3=fromRotation(m.orientation,temp3);
m.sph1+=temp1;
m.sph2+=temp2;
m.sph3+=temp3;
mols.push_back(m);
}
return mols;
}
InitializeItems.h
#ifndef INITIALIZEITEMS_H
#define INITIALIZEITEMS_H
#include <vector>
#include "collidables.h"
std::vector<Sphere> InitializeAtoms(int N, float R);
std::vector<Molecule3> InitializeMol(int N, float R);
#endif
链接器错误:
1>main.obj : error LNK2005: "class irr::core::quaternion __cdecl fromVector(class irr::core::vector3d<float>)" (?fromVector@@YA?AVquaternion@core@irr@@V?$vector3d@M@23@@Z) already defined in InitiliazeItems.obj
1>main.obj : error LNK2005: "class irr::core::vector3d<float> __cdecl purequatToVector(class irr::core::quaternion)" (?purequatToVector@@YA?AV?$vector3d@M@core@irr@@Vquaternion@23@@Z) already defined in InitiliazeItems.obj
1>main.obj : error LNK2005: "class irr::core::vector3d<float> __cdecl fromRotation(class irr::core::quaternion,class irr::core::vector3d<float>)" (?fromRotation@@YA?AV?$vector3d@M@core@irr@@Vquaternion@23@V123@@Z) already defined in InitiliazeItems.obj
1>physics.obj : error LNK2005: "class irr::core::quaternion __cdecl fromVector(class irr::core::vector3d<float>)" (?fromVector@@YA?AVquaternion@core@irr@@V?$vector3d@M@23@@Z) already defined in InitiliazeItems.obj
1>physics.obj : error LNK2005: "class irr::core::vector3d<float> __cdecl purequatToVector(class irr::core::quaternion)" (?purequatToVector@@YA?AV?$vector3d@M@core@irr@@Vquaternion@23@@Z) already defined in InitiliazeItems.obj
1>physics.obj : error LNK2005: "class irr::core::vector3d<float> __cdecl fromRotation(class irr::core::quaternion,class irr::core::vector3d<float>)" (?fromRotation@@YA?AV?$vector3d@M@core@irr@@Vquaternion@23@V123@@Z) already defined in InitiliazeItems.obj
1>visuals.obj : error LNK2005: "class irr::core::quaternion __cdecl fromVector(class irr::core::vector3d<float>)" (?fromVector@@YA?AVquaternion@core@irr@@V?$vector3d@M@23@@Z) already defined in InitiliazeItems.obj
1>visuals.obj : error LNK2005: "class irr::core::vector3d<float> __cdecl purequatToVector(class irr::core::quaternion)" (?purequatToVector@@YA?AV?$vector3d@M@core@irr@@Vquaternion@23@@Z) already defined in InitiliazeItems.obj
1>visuals.obj : error LNK2005: "class irr::core::vector3d<float> __cdecl fromRotation(class irr::core::quaternion,class irr::core::vector3d<float>)" (?fromRotation@@YA?AV?$vector3d@M@core@irr@@Vquaternion@23@V123@@Z) already defined in InitiliazeItems.obj
编辑:添加了链接器错误
edit2:链接到整个项目的github https://github.com/DxsGeorge/Collider
答案 0 :(得分:2)
似乎“已经定义”的问题来自其他三个类(主要的,物理的,视觉效果),所以显示更多关于它们的信息,无论如何我建议你不要使用
#ifndef
#define
#endif
使用
#pragma once
在头文件的开头,它将执行相同的操作(仅在Visual Studio上)并且不会出错。