0

我正在設置旅行推銷員的平坦空間環境。這是我的嘗試:「沒有匹配函數調用'地圖<Flat> ::函數名'

#include <iostream> 
using namespace std; 
#include<stdlib.h> 
#include <cstdlib> 
#include <cmath> 
class Base  //Allocate the memory space 
{ 
protected: 
int n; 
typedef double Coord[2]; 
Coord* city_location; 

Base(int ncities) : n(ncities), city_location(new Coord[n]) {} 
~Base() { delete [] city_location; } 
}; 



template <class T> class Map;  

struct Flat; 
template <> class Map<Flat> : public Base 
{ 
public: 
//int Path[n]; 
Map(int n) : Base(n) 
{ 
    int Path[n];    //Populate with random points for flat version 
    for (int i=0;i<n;i++)  
    { 
    city_location[i][0] = (static_cast <float> (rand())/static_cast <float> (RAND_MAX))*80; 
    city_location[i][1] = (static_cast <float> (rand())/static_cast <float> (RAND_MAX))*80; 
    Path[i] = i; 
    cout << "city " << i << " is at (" << city_location[i][0] << "," << city_location[i][1] << ")\n"; 
    } 

    cout << "\nThe initial path is ("; 

    for(int i=0;i<n;i++) 
    { 
     cout << Path[i]<< " "; 
    } 
    cout<< Path[0] <<")"<<endl; 

pathdistance(Path, n, city_location); //Line 45 

} 

double distance(int i, int j) const  //Pairwise distance function 
{ 
    double dx = city_location[i][0] - city_location[j][0]; 
    double dy = city_location[i][1] - city_location[j][1]; 
    return sqrt(dx*dx+dy*dy); 
} 

double pathdistance(double Path[],int n, double city_location) //total distance function 
{ 
    //cout<< city_location[0][1]; 
    double total = 0; 
    for(int i=0; i<n-1;i++) 
    { 
     total += distance(Path[i],Path[i+1]); 

    } 
     total += distance(Path[n],Path[0]); 

     cout << "total distance is "<< total<<endl; 
     return total; 
     } 

    }; 


int main() 
{ 
    srand(1235); 
    Map<Flat> x(10); 
    cout << "distance between cities 3 and 7 is " << x.distance(3,7) << "\n"; 
} 

我得到的錯誤信息:

45 error: no matching function for call to 'Map<Flat>::pathdistance(int [(((sizetype)(((ssizetype)n) + -1)) + 1)], int&, Base::Coord)' 

我知道,它與我如何將指針的事,但我似乎無法找出正確的方法去做吧。道歉,如果這看起來很醜陋,但我對C++很陌生。在我身上輕鬆一下。提前致謝。

+0

請問您能正確格式化您的代碼嗎?這將不那麼令人困惑。 – alain 2015-02-24 17:42:22

+0

您可以像使用二維數組一樣使用city_location,但是pathdistance期望不變。 – HughB 2015-02-24 17:49:47

+1

'int Path [n];'*非標準C++警報*。您不能使用變量聲明數組作爲條目數。你的編譯器允許它,但它是非標準的。如果你想要標準一致性,請使用'std :: vector '。 – PaulMcKenzie 2015-02-24 17:55:01

回答

0

第45行:函數pathdistance被調用。它要求對某些類型的參數不與你提供給它的參數匹配:

double pathdistance(double Path[],int n, double city_location) 
在地圖中,其中線45的構造

...

pathdistance問他第三個參數是一個double,但city_location是*Coord,或者更確切地說,一個*double[2]

相同的路徑:問一個double[]功能,而且獲得了int[]

0

首先,pathdistance

double pathdistance(double Path[],int n, double city_location); 

函數簽名不符合C++標準。 double Path[]不是猶太教。

至於你的錯誤消息雲:

45 error: no matching function for call to 'Map<Flat>::pathdistance(int [(((sizetype)(((ssizetype)n) + -1)) + 1)], int&, Base::Coord)' 

我可以馬上告訴(甚至沒有看你的源代碼),最後一個參數city_locationBase::Coordinate類型,但你的函數定義,要求它雙倍。

-2

這是C中的一個小程序,它對待旅行推銷員問題。它基於分支和界限算法。爲了生成樹,使用了兩個數據結構:一個堆棧和一個循環隊列。爲了測試的目的,連接矩陣是隨機生成的。出發城市爲1.初始解決方案爲1-n。但實際上,使用由啓發式算法產生的解決方案將會極大地提高程序的性能。

#include <stdio.h> 
int queue[100], stack[100], alt[100], v[100]; 
int sp,head,tail,i,n,g,j,s,path,module,map[100][100]; 
int main() 
{ 
    printf("Number of cities:"); 
    scanf("%d",&n); 
    printf("Max Segment:"); 
    scanf("%d",&module); 
    printf("Seed:"); 
    scanf("%d",&g); 
    srand(g); 
// Generating the sysmetric connection matrix randomly 
for (i=0 ; i<n ; i++) { 
    for (j=i+1 ; j<n ; j++) { 
     map[i][j]= rand() % (module+1); 
     map[j][i]=map[i][j]; 
     } 
for (j=0 ; j<n ; j++) printf("%3d ",map[i][j]); 
printf("\n"); 
    } 
//Start with an initial solution from city 1 
for (i=0 ; i<n ; i++) { 
    queue[i]=i; 
    } 
// Set route length to high value 
    path=module*n; 
    stack[0]=queue[0]; 
    alt[0]=0; 
    printf("running...\n"); 
    sp=0; 
    head=0; 
    tail=n-1; 
    s=0; 
// Explore a branch of the factorial tree 
    while(1) {  
     while(sp<n-1 && s<path) { 
      sp++; 
      head++; if (head==n) head=0; 
      stack[sp]=queue[head]; 
      s=s+map[stack[sp]][stack[sp-1]]; 
      alt[sp]=n-sp-1; 
     } 
// Save a better solution 
     if (s+map[stack[sp]][stack[0]]<path) { 
     path=s+map[stack[sp]][stack[0]]; 
     for (i=0 ; i<n ; i++) v[i]=stack[i]+1; 
     } 
// Leaving nodes when there is no more branches 
     while (alt[sp]==0 && sp>=0) { 
     tail++; if (tail==n) tail=0; 
     queue[tail]=stack[sp]; 
     s=s-map[stack[sp]][stack[sp-1]]; 
     sp--; 
     } 
// If Bottom of stack is reached then stop 
     if (sp<0) break; 
     tail++; if (tail==n) tail=0; 
     queue[tail]=stack[sp]; 
     s=s-map[stack[sp]][stack[sp-1]]; 
// Explore an alternate branch 
     alt[sp]=alt[sp]-1; 
     head++; if (head==n) head=0; 
     stack[sp]=queue[head]; 
     s=s+map[stack[sp]][stack[sp-1]]; 
    } 
    printf("best route=%d\n",path); 
    for (i=0 ; i<n ; i++) printf("%d ",v[i]); 
    printf("%d\n",stack[0]+1); 
    return 0; 
} 

現在讓我們運行n = 10的程序。

[[email protected] ~]$ ./bnb 
Number of cities:10 
Max Segment:345 
Seed:4 
    0 199 171 200 244 241 95 71 71 274 
199 0 15 114 252 72 238 7 258 118 
171 15 0 237 305 343 151 28 274 191 
200 114 237 0 197 158 198 216 342 76 
244 252 305 197 0 292 147 248 98 45 
241 72 343 158 292 0 95 194 116 167 
95 238 151 198 147 95 0 122 83 233 
71 7 28 216 248 194 122 0 28 155 
71 258 274 342 98 116 83 28 0 126 
274 118 191 76 45 167 233 155 126 0 
running... 
best route=735 
1 7 5 10 4 6 2 3 8 9 1 
+0

** - 1 **對於其他一些問題來說,這可能是一個很好的答案,但它只與這個問題有聯繫。爲什麼不在Python中發佈實現?由於該問題標記爲C++,並且涉及OP代碼中的細節。 – 2015-03-20 20:06:24

相關問題