&#34;没有匹配函数来调用&#39; Map <flat> :: functionname&#34; </flat>

时间:2015-02-24 17:31:13

标签: c++ pointers dynamic-memory-allocation traveling-salesman

我正在尝试为旅行推销员设置平面空间环境。这是我的尝试:

#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 ++来说还是一个新手。放轻松我。提前致谢。

3 个答案:

答案 0 :(得分:0)

第45行:正在调用函数pathdistance。它要求某些类型的参数与您提供给它的参数不匹配:

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

在map的构造函数中,第45行是......

pathdistance要求他的第三个参数是double,但city_location是*Coord,或者更准确地说是*double[2]

路径相同:该功能要求double[],但收到int[]

答案 1 :(得分: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_location的类型为Base::Coordinate,但您的函数定义要求它是double。

答案 2 :(得分:-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的程序。

[oldache@localhost ~]$ ./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