[英]C++ Error creating class
我对C ++还是很陌生,直到今天我还没有创建自己的类。 我不希望发布代码供人们正常检查,但是我的截止日期很紧,需要编译我的代码。 我得到三个错误:
-错误:比之前的声明`RobotDeadReckoner :: RobotDeadReckoner()throw()'
-这行有多个标记-错误: RobotDeadReckoner::RobotDeadReckoner()' throws different exceptions - error: definition of implicitly-declared
声明RobotDeadReckoner::RobotDeadReckoner()' throws different exceptions - error: definition of implicitly-declared
RobotDeadReckoner :: RobotDeadReckoner()的定义
-错误:否RobotDeadReckoner::~RobotDeadReckoner()' member function declared in class
RobotDeadReckoner中RobotDeadReckoner::~RobotDeadReckoner()' member function declared in class
代码如下:
#include <cmath>
#include "WPILib.h"
class RobotDeadReckoner
{//<---------------------Error
public:
float getX();
float getY();
float getHeading();
private:
Encoder *encoder1;//Encoder1 (Left Transmision while looking from the back)
Encoder *encoder2;//Encoder2 (Right Transmision while looking from the back)
int wheelRadius;//Wheel Radius (Center Wheel)
float axleWidthCenterToCenter;
int encoderTicksPerRotation;
int transmitionSprocketTeeth;
int wheelSprocketTeeth;
int ticksPerRotation; //ticks per rotation of wheel
float encoderTicks1;
float encoderTicks2;
float pi;
};
RobotDeadReckoner::RobotDeadReckoner()
{//<---------------------Error
wheelRadius = 4;//Wheel Radius (Center Wheel)
axleWidthCenterToCenter = 30+(7/8);
encoderTicksPerRotation = 360;
transmitionSprocketTeeth = 12;
wheelSprocketTeeth = 26;
ticksPerRotation = (wheelSprocketTeeth/transmitionSprocketTeeth)*encoderTicksPerRotation; //ticks per rotation of wheel
encoderTicks1 = encoder1->Get();
encoderTicks2 = encoder2->Get();
pi = atan(1)*4;
}
float RobotDeadReckoner::getX()
{
float x = wheelRadius*cos(getHeading())*(encoderTicks1+encoderTicks2)*(pi/ticksPerRotation);
return x;
}
float RobotDeadReckoner::getY()
{
float y = wheelRadius*sin(getHeading())*(encoderTicks1+encoderTicks2)*(pi/ticksPerRotation);
return y;
}
float RobotDeadReckoner::getHeading()
{
float heading = (2*pi)*(wheelRadius/axleWidthCenterToCenter)*(encoderTicks1-encoderTicks2);
return heading;
}
RobotDeadReckoner::~RobotDeadReckoner()
{ //<---------------------Error
}
我确信这是我不了解c ++的愚蠢简单方法,但是任何帮助将不胜感激!
隐式声明的RobotDeadReckoner :: RobotDeadReckoner()的定义
这是最大的线索。 您尚未声明 RobotDeadReckoner()
的构造函数,只定义了它。 如果您不提供默认的构造函数,则编译器将为您提供一个,因此是“隐式声明”的。 请参阅什么是三规则? 。
没有在classRobotDeadReckoner'中声明的RobotDeadReckoner ::〜RobotDeadReckoner()成员函数
析构函数再次相同。
将以下内容添加到您的类声明的( public:
部分)中:
RobotDeadReckoner();
virtual ~RobotDeadReckoner();
文章的第一部分是一个类定义,它应包含所有成员的声明。 构造函数和析构函数。 这些在您的类定义中不存在。
class Robot{ declarations. }; // end of class definition
以下在您的上述课程中没有相应的声明。
RobotDeadReckoner::RobotDeadReckoner()
另外,您应该将您的课程放在.h文件中,然后
RobotDeadReckoner::RobotDeadReckoner()
在.cpp文件中。
因此,您的课程应类似于:
class RobotDeadReckoner{
RobotDeadReckoner();
~RobotDeadReckoner();
etc...
您应该首先在类定义中声明析构函数(和构造函数)。
class RobotDeadReckoner
{//<---------------------Error
public:
RobotDeadReckoner();
~RobotDeadReckoner(); // <--- HERE
float getX();
float getY();
float getHeading();
private:
Encoder *encoder1;//Encoder1 (Left Transmision while looking from the back)
Encoder *encoder2;//Encoder2 (Right Transmision while looking from the back)
int wheelRadius;//Wheel Radius (Center Wheel)
float axleWidthCenterToCenter;
int encoderTicksPerRotation;
int transmitionSprocketTeeth;
int wheelSprocketTeeth;
int ticksPerRotation; //ticks per rotation of wheel
float encoderTicks1;
float encoderTicks2;
float pi;
};
您RobotDeadReckoner
声明RobotDeadReckoner
构造函数或析构函数,因此无法使用RobotDeadReckoner::RobotDeadReckoner()
和RobotDeadReckoner::~RobotDeadReckoner()
进一步定义它们。
在类声明中,添加
RobotDeadReckoner();
~RobotDeadReckoner();
然后将进一步编译定义。
您只能为用户定义的构造函数和析构函数提供实现:
class RobotDeadReckoner
{
public:
//declare constructor and destructor
RobotDeadReckoner();
~RobotDeadReckoner();
public:
float getX();
float getY();
float getHeading();
private:
Encoder *encoder1;//Encoder1 (Left Transmision while looking from the back)
Encoder *encoder2;//Encoder2 (Right Transmision while looking from the back)
int wheelRadius;//Wheel Radius (Center Wheel)
float axleWidthCenterToCenter;
int encoderTicksPerRotation;
int transmitionSprocketTeeth;
int wheelSprocketTeeth;
int ticksPerRotation; //ticks per rotation of wheel
float encoderTicks1;
float encoderTicks2;
float pi;
};
如果您没有为类声明构造函数或析构函数,则编译器将自动为您生成一个默认值,您无法实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.