-1
我試圖找到通過給定的活動網絡的所有路徑,並認爲我可以這樣做使用遞歸。這就像穿越具有不同文件夾和子文件夾的網絡驅動器一樣。C++遞歸與對象作爲函數參數
我已經定義了一個活動類(用於存儲每個活動的屬性)和一個網絡類(用於存儲網絡活動)。我將網絡對象作爲參數傳遞給遞歸函數調用。遞歸函數不能按預期工作。它給出了分段錯誤,似乎無法正確訪問網絡對象。
有人能指出我如何解決這個問題的正確方向(以及我怎麼也可以將找到的路徑存儲在二維數組中)。
下面是代碼:
#include <iostream>
using namespace std;
class Activity {
public:
int id;
double duration;
int successors[10];
};
class Network {
public:
Activity activities[];
};
int traverse(int, Network);
int main()
{
// define the set of successors for each activity
// a value of -1 indicates that no more successors are present
const int successorset[][5] = {
{ 1,2,3,-1 },
{ 5,-1 },
{ 4,-1 },
{ 4,-1 },
{ 5,-1 },
{ -1},
};
// two dimensional array where for each path the activity ids are stored
int pathset [10][10];
// declare activities
Activity Activity0, Activity1, Activity2, Activity3, Activity4, Activity5;
// declare network
Network NetworkMain;
// define activity 0
Activity0.id = 0;
Activity0.duration = 0;
Activity0.successors[0] = successorset[0][0];
Activity0.successors[1] = successorset[0][1];
Activity0.successors[2] = successorset[0][2];
Activity0.successors[3] = successorset[0][3];
NetworkMain.activities[0] = Activity0;
// define activity 1
Activity1.id = 1;
Activity1.duration = 3;
Activity1.successors[0] = successorset[1][0];
Activity1.successors[1] = successorset[1][1];
NetworkMain.activities[1] = Activity1;
// define activity 2
Activity2.id = 2;
Activity2.duration = 1;
Activity2.successors[0] = successorset[2][0];
Activity2.successors[1] = successorset[2][1];
NetworkMain.activities[2] = Activity2;
// define activity 3
Activity3.id = 3;
Activity3.duration = 2;
Activity3.successors[0] = successorset[3][0];
Activity3.successors[1] = successorset[3][1];
NetworkMain.activities[3] = Activity3;
// define activity 4
Activity4.id = 4;
Activity4.duration = 4;
Activity4.successors[0] = successorset[4][0];
Activity4.successors[1] = successorset[4][1];
NetworkMain.activities[4] = Activity4;
// define activity 5
Activity5.id = 5;
Activity5.duration = 0;
Activity5.successors[0] = successorset[5][0];
NetworkMain.activities[5] = Activity5;
// print info on all activities to check whether they are defined correctly
for(int a = 0; a < 6; a++) {
cout << "id of activity = " << NetworkMain.activities[a].id << endl;
cout << "duration of activity = " << NetworkMain.activities[a].duration << endl;
int s = 0;
while (NetworkMain.activities[a].successors[s]!=-1) {
cout << "successor of activity " << a << " = " << NetworkMain.activities[a].successors[s] << endl;
s++;
}
}
// call recursive function to traverse through all paths of the network
traverse(0, NetworkMain);
return 0;
}
int traverse(int id, Network net)
{
if (net.activities[id].successors[0]==-1) // reached finish activity
{
cout << "reached finish " << endl;
return 1;
}
else
{
cout << "id of activity under investigation " << net.activities[id].id << endl;
int t = 0;
while (net.activities[id].successors[t]!=-1) {
cout << "going to investigate successor " << t << endl;
traverse(net.activities[id].successors[t], net);
t++;
}
}
}
這裏是輸出:
id of activity = 0
duration of activity = 0
successor of activity 0 = 1
successor of activity 0 = 2
successor of activity 0 = 3
id of activity = 1
duration of activity = 3
successor of activity 1 = 5
id of activity = 2
duration of activity = 1
successor of activity 2 = 4
id of activity = 3
duration of activity = 2
successor of activity 3 = 4
id of activity = 4
duration of activity = 4
successor of activity 4 = 5
id of activity = 5
duration of activity = 0
id of activity under investigation -578861888
going to investigate successor 0
id of activity under investigation 0
going to investigate successor 0
id of activity under investigation -581127744
going to investigate successor 0
Segmentation fault (core dumped)
Process returned 139 (0x8B) execution time : 0.087 s
Press ENTER to continue.
編輯:應添加了新手警報,下面是調試器輸出。
Active debugger config: GDB/CDB debugger:Default
Building to ensure sources are up-to-date
Selecting target:
Debug
Adding source dir: /home/home/Desktop/recursion/
Adding source dir: /home/home/Desktop/recursion/
Adding file: /home/home/Desktop/recursion/bin/Debug/recursion
Changing directory to: /home/home/Desktop/recursion/.
Set variable: LD_LIBRARY_PATH=.:
[debug]Command-line: /usr/bin/gdb -nx -fullname -quiet -args /home/home/Desktop/recursion/bin/Debug/recursion
[debug]Working dir : /home/home/Desktop/recursion
Starting debugger: /usr/bin/gdb -nx -fullname -quiet -args /home/home/Desktop/recursion/bin/Debug/recursion
done
[debug]Reading symbols from /home/home/Desktop/recursion/bin/Debug/recursion...done.
[debug](gdb)
[debug]> set prompt >>>>>>cb_gdb:
Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
[debug]>>>>>>cb_gdb:
[debug]> show version
[debug]GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
[debug]Copyright (C) 2016 Free Software Foundation, Inc.
[debug]License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
[debug]This is free software: you are free to change and redistribute it.
[debug]There is NO WARRANTY, to the extent permitted by law. Type "show copying"
[debug]and "show warranty" for details.
[debug]This GDB was configured as "x86_64-linux-gnu".
[debug]Type "show configuration" for configuration details.
[debug]For bug reporting instructions, please see:
[debug]<http://www.gnu.org/software/gdb/bugs/>.
[debug]Find the GDB manual and other documentation resources online at:
[debug]<http://www.gnu.org/software/gdb/documentation/>.
[debug]For help, type "help".
[debug]Type "apropos word" to search for commands related to "word".
[debug]>>>>>>cb_gdb:
[debug]> set confirm off
Debugger name and version: GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
[debug]>>>>>>cb_gdb:
[debug]> set width 0
[debug]>>>>>>cb_gdb:
[debug]> set height 0
[debug]>>>>>>cb_gdb:
[debug]> set breakpoint pending on
[debug]>>>>>>cb_gdb:
[debug]> set print asm-demangle on
[debug]>>>>>>cb_gdb:
[debug]> set unwindonsignal on
[debug]>>>>>>cb_gdb:
[debug]> set print elements 0
[debug]>>>>>>cb_gdb:
[debug]> set disassembly-flavor intel
[debug]>>>>>>cb_gdb:
[debug]> catch throw
[debug]Catchpoint 1 (throw)
[debug]>>>>>>cb_gdb:
[debug]> source /usr/share/codeblocks/scripts/stl-views-1.0.3.gdb
[debug]>>>>>>cb_gdb:
[debug]> directory /home/home/Desktop/recursion/
[debug]Source directories searched: /home/home/Desktop/recursion:$cdir:$cwd
[debug]>>>>>>cb_gdb:
[debug]Using terminal's PID as console PID 13148, TTY /dev/pts/1
[debug]> tty /dev/pts/1
[debug]Queued:[tty /dev/pts/1]
[debug]>>>>>>cb_gdb:
[debug]> run
[debug]Starting program: /home/home/Desktop/recursion/bin/Debug/recursion
[debug]Program received signal SIGSEGV, Segmentation fault.
[debug]0x0000000000400ee9 in traverse (id=-140179936, net=...) at /home/home/Desktop/recursion/main.cpp:103
[debug]/home/home/Desktop/recursion/main.cpp:103:2808:beg:0x400ee9
[debug]>>>>>>cb_gdb:
Program received signal SIGSEGV, Segmentation fault.
At /home/home/Desktop/recursion/main.cpp:103
[debug]> bt 30
[debug]#0 0x0000000000400ee9 in traverse (id=-140179936, net=...) at /home/home/Desktop/recursion/main.cpp:103
[debug]#1 0x0000000000400ff1 in traverse (id=0, net=...) at /home/home/Desktop/recursion/main.cpp:114
[debug]#2 0x0000000000400ff1 in traverse (id=6, net=...) at /home/home/Desktop/recursion/main.cpp:114
[debug]#3 0x0000000000400ff1 in traverse (id=0, net=...) at /home/home/Desktop/recursion/main.cpp:114
[debug]#4 0x0000000000400e91 in main() at /home/home/Desktop/recursion/main.cpp:96
[debug]>>>>>>cb_gdb:
那麼你的調試器說什麼?段錯在哪裏? – melpomene
「活動活動」並沒有至少在你的臉上發出大的警告是令人驚訝的,並且有點令人沮喪。我想你可能想要調查['std :: vector'](http://en.cppreference.com/w/cpp/container/vector) – WhozCraig
我很驚訝你的編譯器接受'Activity activities [];'。你在編譯什麼選項?你有沒有得到任何警告? – melpomene