C ++创建类时出错

时间:2012-02-20 21:49:58

标签: c++ class

我对c ++很新,直到今天才开始创建自己的类。我不喜欢发布代码供人们正常审查,但我的时间紧迫,需要编译我的代码。我得到三个错误:

-error:比之前的声明`RobotDeadReckoner :: RobotDeadReckoner()throw()'

- 此行的多个标记      - 错误:声明RobotDeadReckoner::RobotDeadReckoner()' throws different exceptions - error: definition of implicitly-declared RobotDeadReckoner :: RobotDeadReckoner()'

-error:no 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 ++,但任何帮助都会受到赞赏!

5 个答案:

答案 0 :(得分:1)

  

隐式声明的RobotDeadReckoner :: RobotDeadReckoner()

的定义

这是最大的线索。你没有声明 RobotDeadReckoner()的构造函数,你只是定义它。如果您不提供默认构造函数,编译器将为您提供一个,因此“隐式声明”。请参阅What is The Rule of Three?

  

没有RobotDeadReckoner :: ~RobotDeadReckoner()'成员函数在classRobotDeadReckoner中声明'

析构函数再次相同。

将以下内容添加到您的类声明的(public:部分):

RobotDeadReckoner();
virtual ~RobotDeadReckoner();

答案 1 :(得分:1)

你的帖子的第一部分是一个类定义,它应该包含所有成员的声明。构造函数和析构函数。这些不在您的课程定义中。

 class Robot{   declarations. }; // end of class definition

以下在您的班级中没有相应的声明。

RobotDeadReckoner::RobotDeadReckoner()

此外,您应该将您的课程放在.h文件和

RobotDeadReckoner::RobotDeadReckoner()

在.cpp文件中。

所以你的课应该是这样的:

class RobotDeadReckoner{
     RobotDeadReckoner();
     ~RobotDeadReckoner();
 etc...

答案 2 :(得分:0)

首先应在类定义中声明析构函数(和构造函数)。

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;
};

答案 3 :(得分:0)

您既未声明RobotDeadReckoner构造函数也未声明析构函数,因此您无法使用RobotDeadReckoner::RobotDeadReckoner()RobotDeadReckoner::~RobotDeadReckoner()进一步定义它们。

在类声明中,添加

RobotDeadReckoner();
~RobotDeadReckoner();

然后下面的定义将编译。

答案 4 :(得分:0)

您只能为用户定义的构造函数和析构函数提供实现:

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;
};

如果您没有为您的类声明构造函数或析构函数,编译器将自动为您生成一个默认值,您无法实现。