[BGL] Troubles with floyd_warshall_all_pairs, johnson_all_pairs

Hi, guys! I'm stumbled upon a weird problem using SUBJ. functions! Lets suppose that we don't know how much Vertexes we are going to have (for instance Vertexes might be got from file or stdin). So then, in my opinion, is completely impossible to execute SUBJ functions! Let me explain: =============== int xx; //"xx" is runtime-defined vertices size. double* cmat=new(xx*xx); //"cmat" is adjacency matrix actually, it's also defined during runtime. InitCmat(); //here I fill in "cmat" array typedef adjacency_list<listS, vecS, directedS, no_property, property<edge_weight_t, double> > Graph; //This is from some example Graph G(xx); for (int i=0;i<xx;i++) { for (int j=0;j<xx;j++) { if (cmat[i*xx+j]!=0) { add_edge(i,j,cmat[i*xx+j],G); } } } //and now the most interesting part of the code //AFAIK it's impossible to declare array using directive below in runtime: double [xx][xx] D; //Compiler says the same: "C2057: expected constant expression" //even code: double [(const int)xx][(const int)xx] D; //won't work, so the only option left is to make a pointer: double* D = new [xx*xx] ; //but both "floyd_warshall_all_pairs_shortest_paths" and "johnson_all_pairs_shortest_paths" are declared to take a reference, not a pointer, so you'll get multiple compile errors, if you try to compile like below: floyd_warshall_all_pairs_shortest_paths(G, D); //the instruction above will cause multiple compile errors like this: \boost_1_34_0\boost\graph\floyd_warshall_shortest.hpp(115): error C2109: subscript requires array or pointer type P.S. Compiler is MSVC7.1, but I also read that gcc does the same. P.S.S. Sorry for duplication, if this also appears in [devel] list. =============== 1. What can I do to get this code work? 2. If no workaround is available, don't you think that it's a good idea to change the DistanceMatrix type in prototypes to pointers? Thanks in advance! //Alex.

On Jul 2, 2007, at 6:26 AM, Alex Ivanov wrote:
//AFAIK it's impossible to declare array using directive below in runtime: double [xx][xx] D; //Compiler says the same: "C2057: expected constant expression" //even code: double [(const int)xx][(const int)xx] D; //won't work, so the only option left is to make a pointer: double* D = new [xx*xx] ;
//but both "floyd_warshall_all_pairs_shortest_paths" and "johnson_all_pairs_shortest_paths" are declared to take a reference, not a pointer, so you'll get multiple compile errors, if you try to compile like below:
floyd_warshall_all_pairs_shortest_paths(G, D);
The DistanceMatrix is expected to be subscripted twice, so you would need something like: typedef double* double_array; double_array* D = new double_array[xx]; for (int i = 0; i < xx; ++i) D[i] = new double [xx]; Or, you could use a vector of vectors: vector<vector<double> > D(xx, vector<double>(xx)); - Doug

Hi, Doug! Thanks a lot for your reply, you've solved my issue. Best regards, Alex. "Doug Gregor" <dgregor@osl.iu.edu> wrote in message news:D9C1BCC3-0FD1-490A-BEB6-02A7AEE13AE2@osl.iu.edu...
On Jul 2, 2007, at 6:26 AM, Alex Ivanov wrote:
//AFAIK it's impossible to declare array using directive below in runtime: double [xx][xx] D; //Compiler says the same: "C2057: expected constant expression" //even code: double [(const int)xx][(const int)xx] D; //won't work, so the only option left is to make a pointer: double* D = new [xx*xx] ;
//but both "floyd_warshall_all_pairs_shortest_paths" and "johnson_all_pairs_shortest_paths" are declared to take a reference, not a pointer, so you'll get multiple compile errors, if you try to compile like below:
floyd_warshall_all_pairs_shortest_paths(G, D);
The DistanceMatrix is expected to be subscripted twice, so you would need something like:
typedef double* double_array;
double_array* D = new double_array[xx]; for (int i = 0; i < xx; ++i) D[i] = new double [xx];
Or, you could use a vector of vectors:
vector<vector<double> > D(xx, vector<double>(xx));
- Doug
participants (2)
-
Alex Ivanov
-
Doug Gregor