C++ 创建类时出错

标签 c++ class

我是 c++ 的新手,直到今天才创建自己的类。我不喜欢发布代码供人们正常审查,但我的截止日期很紧,需要编译我的代码。我收到三个错误:

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

-此行有多个标记 - 错误:RobotDeadReckoner::RobotDeadReckoner()' 的声明抛出不同 异常(exception) - 错误:隐式声明的定义RobotDeadReckoner::RobotDeadReckoner()'

-错误:类中没有声明RobotDeadReckoner::~RobotDeadReckoner()'成员函数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++ 但任何帮助将不胜感激!

最佳答案

definition of implicitly-declared RobotDeadReckoner::RobotDeadReckoner()

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

no RobotDeadReckoner::~RobotDeadReckoner()' member function declared in classRobotDeadReckoner'

析构函数也是如此。

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

RobotDeadReckoner();
virtual ~RobotDeadReckoner();

关于C++ 创建类时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9368932/

相关文章:

c++ - 在 C++ 中声明泛型 istream

c++ - 链表深拷贝构造函数

c++ - 如何捕获初始化列表中的异常?

Python:连接两个字符串以从类中获取变量

python - 访问类实例的父模块的 __all__ 列表

java - 是什么让 String 不可变?

C++ 内联单个调用函数?

C++ 在游戏中实现攻击类

javascript - Div 类在滚动时不会改变

C++ 速度和逻辑流程