我对C ++还是很陌生,直到今天我还没有创建自己的类。我不希望发布代码供人们正常检查,但截止日期很紧,需要编译我的代码。我得到三个错误:
-错误:比先前的声明`RobotDeadReckoner :: RobotDeadReckoner()throw()'
-此行有多个标记
-错误:RobotDeadReckoner::RobotDeadReckoner()' throws different exceptions - error: definition of implicitly-declared
RobotDeadReckoner ::: RobotDeadReckoner()'的声明
-错误:否RobotDeadReckoner::~RobotDeadReckoner()' member function declared in class
RobotDeadReckoner'
代码如下:
#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()
的构造函数,只定义了它。如果您不提供默认的构造函数,则编译器将为您提供一个,因此是“隐式声明”的。请参阅What is The Rule of Three?。
没有在classRobotDeadReckoner'中声明的RobotDeadReckoner ::〜RobotDeadReckoner()成员函数
析构函数再次相同。
将以下内容添加到类声明的(public:
部分):
RobotDeadReckoner();
virtual ~RobotDeadReckoner();