2012-02-20 37 views
1

我对C++相当陌生,从未创建过我自己的类,直到今天。我不喜欢发布代码供人们正常查看,但我处于紧急的最后期限之内,需要编译我的代码。我得到三个错误:C++错误创建类

- 错误:比以前的声明`RobotDeadReckoner :: RobotDeadReckoner()掷() '

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

-error:没有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++,但任何帮助将不胜感激!

+1

请注意,getX()和getY()的约定会假设您有一个名为X的变量,另一个名为Y.您既没有 - 让你的getter描述他们得到的东西。 – KevinDTimm 2012-02-20 21:52:27

回答

1

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(); 
+0

谢谢!我不明白这是什么意思,但看到它后,这很明显... – Jacob9706 2012-02-20 21:56:03

+0

@ Jacob9706:高兴地帮忙。欢迎来到StackOverflow :) – Johnsyweb 2012-02-21 01:18:34

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

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

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

以下在上面的类中没有相应的声明。

RobotDeadReckoner::RobotDeadReckoner() 

而且你应该把你的类在.h文件和

RobotDeadReckoner::RobotDeadReckoner() 
在.cpp文件

所以,你的类应该是这样的:

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

您已经声明既不是RobotDeadReckoner构造也不析构函数,所以你不能定义它们进一步与RobotDeadReckoner::RobotDeadReckoner()RobotDeadReckoner::~RobotDeadReckoner()下来。

在类的声明,添加

RobotDeadReckoner(); 
~RobotDeadReckoner(); 

,然后定义进一步下跌将编译。

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

如果你不声明构造或相关类的析构函数,编译器会自动生成一个默认一个给你,你无法实现。