2012-02-20 3 views
1

저는 C++을 처음 접했고 현재까지 자신의 클래스를 만든 적이 없습니다. 사람들이 정상적으로 검토 할 수 있도록 코드를 게시하는 것을 싫어하지만 마감 기한이 만료되어 코드를 컴파일해야합니다.C++ 클래스를 만드는 중 오류가 발생했습니다.

-error : 나는 세 가지 오류를 얻을 이전의 선언보다`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

답변

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

+0

@ Jacob9706 : 도와 줘서 고맙다. StackOverflow에 오신 것을 환영합니다 :) – Johnsyweb

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 파일에 클래스를 넣어해야하며 .cpp 파일에서

RobotDeadReckoner::RobotDeadReckoner() 

. 당신은 RobotDeadReckoner 생성자도 소멸자도를 선언 한

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

이 더 아래로 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; 
}; 

당신이 생성자 또는 클래스의 소멸자, 컴파일러가 자동으로 기본 키를 생성합니다 선언하지 않으면, 당신은 그것을 구현할 수 없습니다.

관련 문제