lundi 28 novembre 2016

Getting errors trying to return a 2D array from a function [duplicate]

This question already has an answer here:

I'm working on a program to find pathways in undirected graphs, and I'm using a specific function to return a 2D array of ints. The problem is, though, that I can't figure out how to make one of the dimensions' size determined at run time. When I try to leave the function declaration as

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

I get this error:

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

Changing it to

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

gives me this error instead:

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

I can't figure out for the life of me how to get this to work. I'd appreciate some help. Here's relevant snippets of the code:

# 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){

Skipped most of main, this is the relevant portion

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;

Then later down at my definition...

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;
}

Aucun commentaire:

Enregistrer un commentaire