C++ 错误C2084“;已经有了身体”;应该编译吗?
我在编译时收到以下错误消息:C++ 错误C2084“;已经有了身体”;应该编译吗?,c++,inheritance,compilation,C++,Inheritance,Compilation,我在编译时收到以下错误消息: mathsphere.cpp(10): error C2084: function 'MathSphere::MathSphere(const Vec3f &,const float &,Shader *)' already has a body mathsphere.h(17) : see previous definition of '{ctor}' mathsphere.cpp(10): error C2512: 'VisualObjec
mathsphere.cpp(10): error C2084: function 'MathSphere::MathSphere(const Vec3f &,const float &,Shader *)'
already has a body mathsphere.h(17) : see previous definition of '{ctor}'
mathsphere.cpp(10): error C2512: 'VisualObject' : no appropriate default constructor available
我的代码如下:
MathSphere.cpp
#include "MathSphere.h"
MathSphere::MathSphere(const Vec3f &c, const float &r, Shader* shadName) {
radius = r;
radiusSquared = (r * r);
}
#include "VisualObject.h"
VisualObject::VisualObject(const Vec3f &pos, Shader* shaderName) {
shader = shaderName;
}
Shader* VisualObject::getShader() {
return shader;
}
MathSphere.h
#ifndef MathSphere_h
#define MathSphere_h
#include "Vec3.h"
#include "VisualObject.h"
class MathSphere : public VisualObject {
public:
Vec3f center;
float radius, radiusSquared;
Vec3f surfaceColor, emissionColor;
Vec3f getNormal(const Vec3f &pHit) const;
bool intersect(const Vec3f &rayOrigin, const Vec3f &rayDir, float &t0, float &t1) const;
MathSphere(const Vec3f &c, const float &r, Shader* shadName) : VisualObject(c, shadName){}
};
#endif
#ifndef VisualObject_h
#define VisualObject_h
#include "Object.h"
class VisualObject : public Object {
public:
Shader * shader;
Shader* getShader();
VisualObject(const Vec3f &pos, Shader* shaderName) : Object(pos) {}
~VisualObject();
virtual bool intersect(const Vec3f &rayOrigin, const Vec3f &rayDir, float &t0, float &t1) const;
virtual Vec3f getNormal(const Vec3f &pHit) const;
};
#endif
VisualObject.cpp
#include "MathSphere.h"
MathSphere::MathSphere(const Vec3f &c, const float &r, Shader* shadName) {
radius = r;
radiusSquared = (r * r);
}
#include "VisualObject.h"
VisualObject::VisualObject(const Vec3f &pos, Shader* shaderName) {
shader = shaderName;
}
Shader* VisualObject::getShader() {
return shader;
}
VisualObject.h
#ifndef MathSphere_h
#define MathSphere_h
#include "Vec3.h"
#include "VisualObject.h"
class MathSphere : public VisualObject {
public:
Vec3f center;
float radius, radiusSquared;
Vec3f surfaceColor, emissionColor;
Vec3f getNormal(const Vec3f &pHit) const;
bool intersect(const Vec3f &rayOrigin, const Vec3f &rayDir, float &t0, float &t1) const;
MathSphere(const Vec3f &c, const float &r, Shader* shadName) : VisualObject(c, shadName){}
};
#endif
#ifndef VisualObject_h
#define VisualObject_h
#include "Object.h"
class VisualObject : public Object {
public:
Shader * shader;
Shader* getShader();
VisualObject(const Vec3f &pos, Shader* shaderName) : Object(pos) {}
~VisualObject();
virtual bool intersect(const Vec3f &rayOrigin, const Vec3f &rayDir, float &t0, float &t1) const;
virtual Vec3f getNormal(const Vec3f &pHit) const;
};
#endif
使用
.h
中的这一行,您为构造函数提供了一个主体
MathSphere(const Vec3f &c, const float &r, Shader* shadName) : VisualObject(c, shadName){}
^^
因此,在.h
文件或源文件中合并这两者
也应该删除进程中的第二个错误,因为在您的头中,实际上给了“代码> VisualObjult/Cuth>构造函数的参数,但在源文件中没有,所以默认的假设是假定的,并且因为您没有任何,它会产生错误。C++中的< /P> < P>,有声明和定义:
- 声明是关于引入一个名称和一些最小的信息
- 定义是关于准确定义名称之外的内容
因此,有了这一点背景:
// MathSphere.h
MathSphere(const Vec3f &c, const float &r, Shader* shadName):
VisualObject(c, shadName){}
// MathSphere.cpp
MathSphere::MathSphere(const Vec3f &c, const float &r, Shader* shadName)
{
radius = r;
radiusSquared = (r * r);
}
这两个都是定义
您有两种解决方案:
- 一个单独的定义,直接在“.h”中(如果在类主体中这样做就可以了)
- “.h”中的声明和“.cpp”中的定义
// MathSphere.h
MathSphere(const Vec3f &c, const float &r, Shader* shadName);
// MathSphere.cpp
MathSphere::MathSphere(const Vec3f &c, const float &r, Shader* shadName):
VisualObject(c, shadName)
{
radius = r;
radiusSquared = (r * r);
}
不过,为了获得额外积分,您应该初始化构造函数的初始值设定项列表中的属性:
MathSphere::MathSphere(const Vec3f &c, const float &r, Shader* shadName):
VisualObject(c, shadName), radius(r), radiusSquared(r*r)
{
}
嗨,JBL,谢谢你的快速回答。你能再详细一点吗?也许是用更正的代码行——这已经困扰了我一点,我对C++非常陌生。非常感谢!注:由于某些原因,一些人可能认为这个问题过于简单,尽管问题很清楚,但已接近解决;如果发生了,请给我留言,我会投票重新打开它。