我正在設置旅行推銷員的平坦空間環境。這是我的嘗試:「沒有匹配函數調用'地圖<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++很陌生。在我身上輕鬆一下。提前致謝。
請問您能正確格式化您的代碼嗎?這將不那麼令人困惑。 – alain 2015-02-24 17:42:22
您可以像使用二維數組一樣使用city_location,但是pathdistance期望不變。 – HughB 2015-02-24 17:49:47
'int Path [n];'*非標準C++警報*。您不能使用變量聲明數組作爲條目數。你的編譯器允許它,但它是非標準的。如果你想要標準一致性,請使用'std :: vector'。 –
PaulMcKenzie
2015-02-24 17:55:01