2012-05-01 26 views
-4

我試圖編寫Prim的算法。然而,當我編譯我得到的線路上的錯誤:57,59,60,62,63,65錯誤:在算術中使用的函數指針

顯然這是與我的陣列有關......我只是不知道究竟是什麼。我感覺到它與我試圖找出並失敗的指針有關。

int map [] [7]是我用來跟蹤我的初始圖形的全局數組。

編輯 - 這裏要求的是所有的代碼

#include <iostream> 

using namespace std; 

void performPrims(int); 
void print(int *); 

// Global Variables 
int nodes = 7; 
int cur = 0; 
int cost = 0; 

int map[][7] = 
    {{0,3,6,0,0,0,0}, 
    {3,0,7,0,0,12,0}, 
    {6,7,0,10,13,8,0}, 
    {0,0,10,0,0,0,5}, 
    {0,0,13,0,0,9,4}, 
    {0,12,8,0,9,0,11}, 
    {0,0,0,5,4,11,0}}; 

int main() 
{ 
// Initializations 

srand(time(NULL)); 
int S = rand() % 7; // Create an arbitrary starting point 

// Perform Prim's Algorithm starting with S 

performPrims(S); 

return 0; 
} 

void performPrims(int S) 
{ 
int low = 100; 

// open[] is used to keep track of what vertices are available 
int open [] = {1,2,3,4,5,6,7}; 

// closed [] is used to keep track of what vertices have been reached and in what order 
int closed [] = {0,0,0,0,0,0,0}; 

open [S] = 0;  // Make our starting node unavailable 
closed [cur] = S+1; // Record our starting node in the path 
cur++; 

while (cur < 7) 
{ 
    int i=0; 
    while (i<nodes) 
    { 
     int k=0; 
     while (k<nodes) 
     { 
      if (*map[*close[i]-1][k] > 0 && *map[*close[i]-1][k] < low) 
      { 
       low = map[close[i]-1][k]; 
       close [cur] = k+1; 
      } 
      map[close[cur-1]-1][close[cur]-1] = 0; 
      map[close[cur]-1][close[cur-1]-1] = 0; 
      cost += low; low = 100; 
      open[close[cur]-1] = 0; 
      k++; 
     } 
     i++; 
    } 
    cur++; 
} 
print(closed); 
} 

void print(int *closed) 
{ 
cout<<"The path taken was "; 
for (int i=0; i<nodes; i++) 
    cout<<closed[i]<<" "<<endl; 
cout<<"\nThe cost is "<<cost<<endl; 
} 

錯誤

有一些不同的:

錯誤:指針的函數運算使用(許多行) 錯誤:無效類型'int [7] [7] [int(*)(int)]'數組下標(許多行) 錯誤:a指定只讀位置(LINE 60) 錯誤:無法在作業(LINE 60)中將'int'轉換爲'int()(int)'

+0

你會得到什麼錯誤? – deebee

+0

猜測第57行是你的例子的第一行,那麼它看起來像'close'是什麼導致了這個問題。你能向我們展示所有相關的代碼和錯誤信息嗎? – Fraser

+0

這些變量是如何定義的?什麼是確切的錯誤? – smocking

回答

3

拼寫錯誤closed。您改爲輸入close

close的所有實例替換爲closed

此外,將#include <cstdlib>添加到文件的頂部。

+0

這是令人尷尬的...但是謝謝你。我希望我可以給你寄一盒油炸圈餅或小貓 –

+0

和'',他需要刪除'*'行if'(* map [* close [i] -1] [k]> 0 && * map [* close [i] -1] [k]

+0

我已經刪除了*。謝謝。現在我只需要讓代碼工作。 –