在标头中声明变量时,未在范围中声明变量 我正在用C++制作一个TIC TAC游戏。我在标题中声明了这个变量slot1,在cpp中我尝试使用这个变量,但它显示错误:“slot1”没有在这个范围中声明 //main.cpp #include <iostream> #include <string> #include "TicTacToe.h" using namespace std; TicTacToe gameOn; int main() { gameOn.startGame(); return 0; } //TicTacToe.h #ifndef TICTACTOE_H #define TICTACTOE_H class TicTacToe { public: void startGame(); void setupGame(); //As you can see slot1 is clearly declared char s1ot1 = '1'; char slot2 = '2'; char slot3 = '3'; char slot4 = '4'; char slot5 = '5'; char slot6 = '6'; char slot7 = '7'; char slot8 = '8'; char slot9 = '9'; }; #endif // TICTACTOE_H //TicTacToe.cpp #include <iostream> #include "TicTacToe.h" #include <stdlib.h> #include <string> using namespace std; string mode; void TicTacToe::startGame() { system("cls"); cout << "This is a Tic Tac Toe game \n This game needs 2 players" << endl; while(true){ cout << "Will you play? (Yes/No)" << endl; cin >> mode; if(mode == "Yes"){ break; }else if(mode == "No"){ cout << "Thank you for choosing this game..." << endl; exit(0); break; }else{ cout << "Your input is invalid" << endl; } } setupGame(); } void TicTacToe::setupGame(){ cout << slot1 << " | " << slot2 << " | " << slot3 << endl; //slot1 variable "was not declared" cout << "----------------------" << endl; cout << slot4 << " | " << slot5 << " | " << slot6 << endl; cout << "----------------------" << endl; cout << slot7 << " | " << slot8 << " | " << slot9 << endl; }

在标头中声明变量时,未在范围中声明变量 我正在用C++制作一个TIC TAC游戏。我在标题中声明了这个变量slot1,在cpp中我尝试使用这个变量,但它显示错误:“slot1”没有在这个范围中声明 //main.cpp #include <iostream> #include <string> #include "TicTacToe.h" using namespace std; TicTacToe gameOn; int main() { gameOn.startGame(); return 0; } //TicTacToe.h #ifndef TICTACTOE_H #define TICTACTOE_H class TicTacToe { public: void startGame(); void setupGame(); //As you can see slot1 is clearly declared char s1ot1 = '1'; char slot2 = '2'; char slot3 = '3'; char slot4 = '4'; char slot5 = '5'; char slot6 = '6'; char slot7 = '7'; char slot8 = '8'; char slot9 = '9'; }; #endif // TICTACTOE_H //TicTacToe.cpp #include <iostream> #include "TicTacToe.h" #include <stdlib.h> #include <string> using namespace std; string mode; void TicTacToe::startGame() { system("cls"); cout << "This is a Tic Tac Toe game \n This game needs 2 players" << endl; while(true){ cout << "Will you play? (Yes/No)" << endl; cin >> mode; if(mode == "Yes"){ break; }else if(mode == "No"){ cout << "Thank you for choosing this game..." << endl; exit(0); break; }else{ cout << "Your input is invalid" << endl; } } setupGame(); } void TicTacToe::setupGame(){ cout << slot1 << " | " << slot2 << " | " << slot3 << endl; //slot1 variable "was not declared" cout << "----------------------" << endl; cout << slot4 << " | " << slot5 << " | " << slot6 << endl; cout << "----------------------" << endl; cout << slot7 << " | " << slot8 << " | " << slot9 << endl; },c++,C++,我更改了数据类型并运行良好,但问题是这个特定变量,因为如果我完全删除它,其他字符将被正常接受,它们的写入方式与slot1完全相同。我的代码怎么了

我更改了数据类型并运行良好,但问题是这个特定变量,因为如果我完全删除它,其他字符将被正常接受,它们的写入方式与slot1完全相同。我的代码怎么了

<我是C++新手,请尽量具体。

你声明S1OT1不是SLUT1通知1One而不是L:

char s1ot1 = '1';
改为

char slot1 = '1';

打字错误slot1!=S1OT1您从未声明过名为slot1的变量。您确实声明了一个名为s1ot1的变量,但没有使用它。这让我想起,我花了几个小时跟踪复杂的链接器错误,这些错误与复杂的跨平台调用约定和导出有关,结果却发现未解析的外部函数没有在任何地方实现。完全可以。`//正如您所看到的,slot1清楚地声明为`-或notOK gang。别再丢脸了是的我想。。。我现在觉得有点傻。。。无论如何,谢谢你: