第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

學習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 */這個一直報錯請問是什么問題

正在回答

1 回答

不知道你全部的代碼是怎樣的。提供一個樣本僅供參考吧。

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

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
C++遠征之封裝篇(上)
  • 參與學習       103386    人
  • 解答問題       732    個

封裝--面向?qū)ο蟮幕?,本教程力求幫助小伙伴們即學即會

進入課程

學習c++的類中遇到的一個問題

我要回答 關(guān)注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號