我對 C 相當陌生,目前正在通過認證來學習這種語言。我以前只使用過 Python 等語言。
我發現了類似的帖子,但沒有一個與我的代碼有關。
我有以下代碼來創建一個十六進制游戲。我試圖有一個簡單的功能來顯示每次玩家移動時的棋盤。
一開始我盡量保持代碼盡可能簡單(限制指標和庫的使用)。
我有這個錯誤: hex_game.cpp:9:47: error: expected unqualified-id before 'int' 9 | void display_current_array(array[size][size], int size){ |
以下是我的代碼,希望有人能提供幫助:
#include <iostream>
#include <stdlib.h>
using namespace std;
#include <vector>
#include <array>
// a void function to display the array after every moove
void display_current_array(array[size][size], int size){
//s=arr.size();
for (int i = 0; i < size; i )
{
for (int j = 0; j < size; j )
{
cout<<array[i][j]<<endl;
}
}
}
int main(){
// the HEX game is a game of size cases represented by an array either filled with a color;
// int size =11; //as asked but we can play on a differnt board
int size;
// ask the player to give a board size
cout << "What is the size of your Hex Board ? ";
cin>> size;
// create the array to represent the board
int array[size][size];
// the player will choose colors that we will store as an enum type
enum colors {BLUE, RED};
// initialize the array: all positions are 0
for (int i = 0; i < size; i )
{
for (int j = 0; j < size; j )
{
array[i][j]=0;
}
}
display_current_array(array, size);
}
uj5u.com熱心網友回復:
在標準 C 中,陣列的大小必須是編譯時間常數。所以當你寫:
int size;
cin>> size;
int array[size][size]; //NOT STANDARD C
該陳述句array[size][size];不是標準的 c 。
第二,當你寫道:
void display_current_array(array[size][size], int size){
//...
}
請注意,在函式的第一個引數中,display_current_array您沒有指定陣列包含的元素的型別。所以這將導致你得到的錯誤。
解決方案
避免這些并發癥的更好方法是使用動態大小的容器std::vector,如下所示:
#include <iostream>
#include <vector>
// a void function to display the vector after every moove
void display_current_array(const std::vector<std::vector<int>> &vec){//note the vector is passed by reference to avoid copying
for(auto &row: vec)
{
for(auto &col: row)
{
std::cout << col ;
}
std::cout<<std::endl;
}
}
int main(){
int size;
// ask the player to give a board size
std::cout << "What is the size of your Hex Board ? ";
std::cin>> size;
// create the 2D STD::VECTOR with size rows and size columns to represent the board
std::vector<std::vector<int>> vec(size, std::vector<int>(size));
// the player will choose colors that we will store as an enum type
enum colors {BLUE, RED};
//NO NEED TO INITIALIZE EACH INDIVIDUAL CELL IN THE 2D VECTOR SINCE THEY ARE ALREADY INITIALIED TO 0
display_current_array(vec);
}
注意:
- 我們不需要將向量的大小作為引數傳遞
- 向量通過參考傳遞以避免復制
上述程式的輸出可以在這里看到。
uj5u.com熱心網友回復:
你不是在問問題,只是提到一個錯誤。
因此,這是避免該錯誤的方法:
void display_current_array(int array[size][size], int size)
{}
當然,它會給您帶來另一個錯誤:
error: 'size' was not declared in this scope
void display_current_array(int array[size][size], int size)
這就是 π?ντα ?ε? 在評論中所暗示的。
所以,避免可變大小陣列的嘗試。
void display_current_array(int array[][], int size)
{}
這讓你
main.cpp:5:40: error: declaration of 'array' as multidimensional array must have bounds for all dimensions except the first
void display_current_array(int array[][], int size)
所以……再插入一個size?不,出于同樣的原因,它被洗掉了。您可以擺脫該錯誤:
void display_current_array(int array[][42], int size)
{}
但可以肯定的是,這正是您不想要的(恒定大小)。
所以我們得到了 BoP 的評論,你應該(或者甚至嘗試)使用std::array. 一旦明確決定(與嘗試 C 風格的可變長度陣列相反,這在 C 中是不允許的),只需查找教程以獲取詳細資訊。
uj5u.com熱心網友回復:
撰寫此代碼的更好方法是使用 STL 向量容器。如果你想用陣列來做,你需要了解指標。
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
// a void function to display the array after every moove
void display_current_array(vector<vector<int>> &array, int size){ // & means pass-by-reference
//s=arr.size();
for (int i = 0; i < size; i )
{
for (int j = 0; j < size; j )
{
cout << array[i][j] << endl;
}
}
}
int main(){
// the HEX game is a game of size cases represented by an array either filled with a color;
// int size =11; //as asked but we can play on a differnt board
int size;
// ask the player to give a board size
cout << "What is the size of your Hex Board ? ";
cin >> size;
// create the array to represent the board
vector<vector<int>> array;
// the player will choose colors that we will store as an enum type
enum colors {BLUE, RED};
// initialize the array: all positions are 0
for (int i = 0; i < size; i )
{
vector<int> row;
for (int j = 0; j < size; j )
{
row.push_back(0);
}
array.push_back(row);
}
display_current_array(array, size);
}
在這里,我使用 & 運算元通過參考傳遞了向量。
void display_current_array(vector<vector<int>> &array, int size)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/418754.html
標籤:
上一篇:Python回傳最大字典條目
