尝试从函数返回2D数组时出错

时间:2016-11-29 01:49:54

标签: c++ c++11

我正在研究一个在无向图中找到路径的程序,我正在使用一个特定的函数来返回一个二维的int数组。但问题是,我无法弄清楚如何在运行时确定其中一个尺寸的大小。当我尝试将函数声明保留为

int prim(int in[][3], int nsteps, int nedges);

我收到此错误:

error: incompatible types in assignment of ‘int’ to ‘int [(((sizetype)(((ssizetype)(nvertex + -1)) + -1)) + 1)][3]’
     ov = prim(v, nvertex-1, nedge);

将其更改为

int prim[][3](int in[][3], int nsteps, int nedges);

反而给了我这个错误:

p5.cpp:23:4: error: expected unqualified-id before ‘[’ token
int[][3] prim(int in[][3], int nsteps, int nedges);
   ^

我无法弄明白我的生活如何让它发挥作用。我很感激一些帮助。以下是代码的相关摘要:

# include <iostream>
# include <fstream>
# include <string>
using namespace std;

int crimMin(int in[][3], int nsteps, int nedges);

int crimMax(int in[][3], int nsteps, int nedges);

int prim(int in[][3], int nsteps, int nedges);

int main(int argc, char** argv){

跳过大部分主要内容,这是相关部分

else if (x == cmax){
for (i = 0; i < nvertex-1; i++){
    if (v[i][2] == 1) v[i][2] = 0;
    else if (v[i][2] == 0) v[i][2] = 1;
}
ov = prim(v, nvertex-1, nedge);
for (i = 0; i < nvertex-1; i++){
    if (ov[i][2] == 1) ov[i][2] = 0;
    else if (ov[i][2] == 0) ov[i][2] = 1;
}
for (i = 0; i < nvertex-1; i++){
    cout << ov[i][0] << " " << ov[i][1] << " ";
    if (ov[i][2] == 1) cout << "C" << endl;
    else if (ov[i][2] == 0) cout << "W" << endl;
}
return 0;

然后我的定义......

int prim(int in[][3], int nsteps, int nedges){
    int step = 0, curr = 0;
    int i;
    int out[nsteps][3];
    int unvisited[nsteps];
    for (i = 0; i < nsteps; i++) unvisited[i] = 1;{
    //Building the output...
    }
    return out;
}

0 个答案:

没有答案