0
我正在試圖製作一個基本程序,允許用戶在其他地方玩對方,現在我只是試圖初始化一個空數組董事會和顯示它。我曾多次使用過類,而且我的代碼看起來與我所做的其他類沒有什麼不同,但我找不到爲什麼我的函數沒有正確聲明。的錯誤是:函數'在此範圍內未聲明'在C++中使用類時
othello.cpp: In function ‘int main()’:
othello.cpp:9:17: error: ‘generateBoard’ was not declared in this scope
generateBoard();
^
othello.cpp:10:13: error: ‘addBorder’ was not declared in this scope
addBorder();
^
othello.cpp:11:18: error: ‘displayOthello’ was not declared in this
scope
displayOthello();
^
make: *** [othello.o] Error 1
這裏是othelloboard.h
const int boardSize = 4;
class othelloboard{
public:
othelloboard();
~othelloboard();
void generateBoard();
void addBorder();
void displayOthello();
private:
char othelloArray[boardSize][boardSize];
};
這裏是othelloboard.cpp
#include <iostream>
using namespace std;
#include "othelloboard.h"
//Initialize global variables
extern const int boardSize;
othelloboard::othelloboard(){}
othelloboard::~othelloboard(){}
void othelloboard::generateBoard(){
for (int i=1; i<= boardSize; i++){
for (int j=1; j<= boardSize; j++){
othelloArray[i][j] = '.';
}
}
}
void othelloboard::addBorder(){
for (int i=1; i<= boardSize; i++){
char temp = (char)i + '0';
othelloArray[0][i] = temp;
othelloArray[i][0] = temp;
}
}
void othelloboard::displayOthello(){
for (int i=0; i<= boardSize; i++){
for (int j=0; j<= boardSize; j++){
cout << othelloArray[i][j];
}
cout << endl;
}
}
這裏是Othello.cpp
#include <iostream>
using namespace std;
#include "othelloboard.h"
int main(){
extern const int boardSize;
cout << boardSize << endl;
generateBoard();
addBorder();
displayOthello();
}
我也知道全局變量不是最大的,b我們被指示使用全局變量來顯示電路板尺寸。
難道你不在任何地方實例化你的「othelloboard」類嗎? –
將generateBoard的代碼移到構造函數中,然後刪除該方法。這會讓使用更自然。 –
另外'(char)i'不能做你想做的事。 –