學習c++的類中遇到的一個問題
class Position {
public: int x; int y;
};
class Robot {
public:
Robot(); ? ? ? ? ? ? ? ? ? ? ? ? ? /* default constructor, initialize at (0,0) */
Robot(Position pos); ? ? /* initialize at pos */
void Move(char Dir); ? ? /* Dir could be 'N', 'E', 'S', 'W', for other characters, the robot don’t move */
Position GetPosition(); ? ? ? ?/* return current position */
private:
Position currentPos;
};
Robot::Robot(Position pos); ? ? /* initialize at pos */這個一直報錯請問是什么問題
2016-03-16
不知道你全部的代碼是怎樣的。提供一個樣本僅供參考吧。
#include <iostream>
#include <map>
using namespace std;
class Position {
?public:
???????? int x;
???????? int y;
};
class Robot {
public:
???????? Robot();?????????????????????????? /* default constructor, initialize at (0,0) */
???????? Robot(Position pos);???? /* initialize at pos */
???????? void Move(char Dir);???? /* Dir could be 'N', 'E', 'S', 'W', for other characters, the robot don’t move */
???????? Position GetPosition();??????? /* return current position */
private:
???????? Position currentPos;
};
Robot::Robot(){
?currentPos.x=0;
?currentPos.y=0;
}
Robot::Robot(Position pos){
?currentPos.x=pos.x;
?currentPos.y=pos.y;
}
void Robot::Move(char Dir){
?if(Dir=='N') currentPos.y++;
?else if(Dir=='S') currentPos.y--;
?else if(Dir=='E') currentPos.x++;
?else if(Dir=='W') currentPos.x--;
}
Position Robot::GetPosition(){
?return currentPos;
}
// your code will be here
int main() {
??? Position c;
??? c.x = 0;
??? c.y = 1;
??? Robot a;
??? cout << a.GetPosition().x << ' ' << a.GetPosition().y << endl;
??? Robot b( c );
??? cout << b.GetPosition().x << ' ' << b.GetPosition().y << endl;
??? b.Move('E');
??? cout << b.GetPosition().x << ' ' << b.GetPosition().y << endl;
??? b.Move('N');
??? cout << b.GetPosition().x << ' ' << b.GetPosition().y << endl;
??? b.Move('W');
??? cout << b.GetPosition().x << ' ' << b.GetPosition().y << endl;
??? b.Move('S');
??? cout << b.GetPosition().x << ' ' << b.GetPosition().y << endl;
??? b.Move('s');
??? cout << b.GetPosition().x << ' ' << b.GetPosition().y << endl;
??? return 0;
}