C++ 在函数C++;

C++ 在函数C++;,c++,function,C++,Function,当我编译这段代码(如下)时,它会给我一个错误: prog.cpp: In function 'int main()': prog.cpp:46:37: error: cannot convert 'int (*)[n]' to 'int**' for argument '4' to 'void dfs(int, std::vector<int>&, int, int**)' dfs(1, used, n, Adjacency_matrix); prog.cpp:在

当我编译这段代码(如下)时,它会给我一个错误:

prog.cpp: In function 'int main()':
prog.cpp:46:37: error: cannot convert 'int (*)[n]' to 'int**' for argument '4' to 'void dfs(int, std::vector<int>&, int, int**)'
     dfs(1, used, n, Adjacency_matrix);
prog.cpp:在函数“int main()”中:
prog.cpp:46:37:错误:无法将参数“4”的“int(*)[n]”转换为“int**”,将其转换为“void dfs(int,std::vector&,int,int**”
dfs(1,已用,n,邻接矩阵);
据我所知,这是因为对二维质量函数的调用不正确。我需要在这段代码中更正的内容,以便函数接受我的大量代码

p、 对不起我的英语:)

#包括
#包括
#包括
#包括
void dfs(int i,std::vector&used,int n,int(&邻接矩阵)[n][n]){
使用[i]=1;
对于(int j=0;j>n>>k;
int坐标[n][2];
对于(int i=0;istd::cout您的
dsf
函数声明错误。请使用

template<size_t N>
void dfs(int i, std::vector <int> &used, int (&Adjacency_matrix)[N][N]) 
{ ... }

无效,除非
n
const
constepr
。因此,无法从
cin
读取它。如果需要动态大小,请使用
std::vector
或指针。

dsf
函数声明错误。使用

template<size_t N>
void dfs(int i, std::vector <int> &used, int (&Adjacency_matrix)[N][N]) 
{ ... }

无效,除非
n
const
constepr
。因此,您无法从
cin
读取它。如果您想要动态大小,请使用
std::vector
或指针。

您应该将此代码:

int Adjacency_matrix[n][n];
标准C++ C++中,不能使用变量作为数组的数量声明数组。 您可以使用
std::vector
生成int的二维数组

#include <vector>
//...
typedef std::vector<int> Int1D;
typedef std::vector<Int1D> Int2D;
//...
void dfs(int i, Int1D &used, int n, Int2D& Adjacency_matrix) // 
{
//...
}

int main()
{
    //...
    Int2D coordinates(n, Int1D(2));       // replacement for int coordinates[n][2]
    //...
    Int2D Adjacency_matrix(n, Int1D(n));  // substitute for int Adjacency_matrix[n][n]
    //...
    dfs(1, used, n, Adjacency_matrix);
    //...
}
#包括
//...
typedef std::vector Int1D;
typedef std::vector Int2D;
//...
void dfs(inti、Int1D和used、intn、Int2D和邻接矩阵)//
{
//...
}
int main()
{
//...
Int2D坐标(n,Int1D(2));//替换int坐标[n][2]
//...
Int2D邻接_矩阵(n,Int1D(n));//替换int邻接_矩阵[n][n]
//...
dfs(1,已用,n,邻接矩阵);
//...
}

您应该打开此代码:

int Adjacency_matrix[n][n];
标准C++ C++中,不能使用变量作为数组的数量声明数组。 您可以使用
std::vector
生成int的二维数组

#include <vector>
//...
typedef std::vector<int> Int1D;
typedef std::vector<Int1D> Int2D;
//...
void dfs(int i, Int1D &used, int n, Int2D& Adjacency_matrix) // 
{
//...
}

int main()
{
    //...
    Int2D coordinates(n, Int1D(2));       // replacement for int coordinates[n][2]
    //...
    Int2D Adjacency_matrix(n, Int1D(n));  // substitute for int Adjacency_matrix[n][n]
    //...
    dfs(1, used, n, Adjacency_matrix);
    //...
}
#包括
//...
typedef std::vector Int1D;
typedef std::vector Int2D;
//...
void dfs(inti、Int1D和used、intn、Int2D和邻接矩阵)//
{
//...
}
int main()
{
//...
Int2D坐标(n,Int1D(2));//替换int坐标[n][2]
//...
Int2D邻接_矩阵(n,Int1D(n));//替换int邻接_矩阵[n][n]
//...
dfs(1,已用,n,邻接矩阵);
//...
}

我首先是在C++代码中不使用非标准VLA,我从C++代码中不使用非标准VLA开始。