Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c++;:通过引用传递2d数组? 我回到C++,并且在如何将2D数组传递给函数时遇到了麻烦。下面的代码是我当前的尝试,我已经能够通过使用通过引用传递向量字符串: vector<string> g_dictionary; getDictionaryFromFile(g_dictionary, "d.txt"); ... void getDictionaryFromFile(vector<string> &g_dictionary, string fileName){..} 向量g_字典; getDictionaryFromFile(g_dictionary,“d.txt”); ... void getDictionaryFromFile(向量&g_字典,字符串文件名){..}_C++_Arrays_Pointers_Parameter Passing - Fatal编程技术网

c++;:通过引用传递2d数组? 我回到C++,并且在如何将2D数组传递给函数时遇到了麻烦。下面的代码是我当前的尝试,我已经能够通过使用通过引用传递向量字符串: vector<string> g_dictionary; getDictionaryFromFile(g_dictionary, "d.txt"); ... void getDictionaryFromFile(vector<string> &g_dictionary, string fileName){..} 向量g_字典; getDictionaryFromFile(g_dictionary,“d.txt”); ... void getDictionaryFromFile(向量&g_字典,字符串文件名){..}

c++;:通过引用传递2d数组? 我回到C++,并且在如何将2D数组传递给函数时遇到了麻烦。下面的代码是我当前的尝试,我已经能够通过使用通过引用传递向量字符串: vector<string> g_dictionary; getDictionaryFromFile(g_dictionary, "d.txt"); ... void getDictionaryFromFile(vector<string> &g_dictionary, string fileName){..} 向量g_字典; getDictionaryFromFile(g_dictionary,“d.txt”); ... void getDictionaryFromFile(向量&g_字典,字符串文件名){..},c++,arrays,pointers,parameter-passing,C++,Arrays,Pointers,Parameter Passing,但是,当我尝试对我的2d数组(如下所示)执行相同的操作时,我在“solve_point(boardEx);”行上得到一个错误,该行表示char类型的引用&不能用boardEx[5][4]类型的值初始化 #include <stdio.h> #include <string> using namespace std; void solve_point(char* &board){ printf("solve_point\n"); //boa

但是,当我尝试对我的2d数组(如下所示)执行相同的操作时,我在“solve_point(boardEx);”行上得到一个错误,该行表示char类型的引用&不能用boardEx[5][4]类型的值初始化

#include <stdio.h>   
#include <string>
using namespace std;

void solve_point(char* &board){ 
    printf("solve_point\n");
    //board[2][2] = 'c';
}

int main(){
    char boardEx[5][4];
    solve_point(boardEx);
}
#包括
#包括
使用名称空间std;
无效解算_点(字符*&板){
printf(“解决点”\n);
//董事会[2][2]=“c”;
}
int main(){
char boardEx[5][4];
求解_点(boardEx);
}

类型
char*&
是指针的引用。“2d”数组衰减为指向数组的指针

对于您的阵列
boardEx
它将衰减为
char(*)[4]
类型,该类型需要是您的函数接受的类型:

void solve_point(char (*board)[4]) { ... }

也可以使用模板来推断数组维度

template<size_t M, size_t N>
void solve_point(char (&board)[M][N]) { ... }
或使用:

std::vector boardEx(5,std::vector(4));
...

void solve_point(std::vector可以使用以下方法定义对二维数组的引用:

char (&ref)[5][4] = boardEx;
您可以将函数更改为使用相同的语法

void solve_point(char (&board)[5][4]){ 
    printf("solve_point\n");
    //board[2][2] = 'c';
}

对于动态分配的数组,最好使用
std::vector

int width = 7;
int height = 9;
char boardEx[width][height];
是由一些编译器支持的,但不是标准C++,而是使用:

int width = 7;
int height = 9;
std::vecotr<std::vector<char>> boardEx(width, std::vector(height));
int-width=7;
整数高度=9;
标准::矢量boardEx(宽度,标准::矢量(高度));

相应地更新
solve\u point

您可以通过值或引用声明接受数组的函数

例如(按值)

或(通过引用)

在这两种情况下,都可以使用如下表达式访问数组的元素

board[i][j] = 'c';
请记住,如果您有一个多维数组,例如

T a[N1][N2][N3];
如果
T
是某种类型说明符,那么可以用以下方法重写声明

T ( a[N1] )[N2][N3];
现在,要获得指向数组元素的指针,只需将
(a[N1])
替换为
(*pa)

要获取对数组的引用,请重写其声明,如

T ( a )[N1][N2][N3];
并用
(a)
代替
(&ra)
类似

T ( &ra )[N1][N2][N3] = a;
如果您想编写一个函数,通过引用接受不同大小的二维数组,那么您可以编写

template <typename T, size_t M, size_t N>
void solve_point( T ( &board )[M][N] ){ 
    //...
}

int main(){
    char boardEx[5][4];
    solve_point(boardEx);
}
模板
无效解算点(T(&board)[M][N]){
//...
}
int main(){
char boardEx[5][4];
求解_点(boardEx);
}

谢谢,我忘了在我的问题中包含我试图为一个动态分配的数组做这个。我编辑我的问题来反映我的TeDeX是用两个int变量初始化的,而不是硬编码的数字。我们的编辑使你的代码无效C++,因为C++没有。这使得我的代码> STD::向量< /C>解决方案是唯一的POSS。可移植的标准解决方案。请不要编辑您的帖子,以使回复原始帖子的答案过时。还原代码以使正确答案再次正确。与其使现有问题无效,不如提出新问题。
T ( a[N1] )[N2][N3];
T ( *pa )[N2][N3] = a;
T ( a )[N1][N2][N3];
T ( &ra )[N1][N2][N3] = a;
template <typename T, size_t M, size_t N>
void solve_point( T ( &board )[M][N] ){ 
    //...
}

int main(){
    char boardEx[5][4];
    solve_point(boardEx);
}