Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ &引用;对';地图<;平坦>;::functionname";_C++_Pointers_Dynamic Memory Allocation_Traveling Salesman - Fatal编程技术网

C++ &引用;对';地图<;平坦>;::functionname";

C++ &引用;对';地图<;平坦>;::functionname";,c++,pointers,dynamic-memory-allocation,traveling-salesman,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* c

我正在尝试为旅行推销员建立一个平坦的空间环境。这是我的尝试:

#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";
 }
#包括
使用名称空间std;
#包括
#包括
#包括
类基类//分配内存空间
{
受保护的:
int n;
typedef双坐标[2];
坐标*城市位置;
基地(国际城市):n(城市),城市位置(新合作[n]){}
~Base(){delete[]city_location;}
};
模板类映射;
结构平面;
模板类映射:公共基
{
公众:
//int路径[n];
地图(整数n):基准(n)
{
int路径[n];//为平面版本填充随机点

对于(int i=0;i第45行:正在调用函数pathdistance。它要求某些类型的参数与您提供给它的参数不匹配:

double pathdistance(double Path[],int n, double city_location)
在map的构造函数中,其中第45行是

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


路径相同:函数要求一个
double[]
,但首先收到一个
int[]
,函数签名为
pathdestance

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

不符合C++标准。<代码>双路径[]/COD>不是KoSHER。< /P> 就您的错误消息而言:

 45 error: no matching function for call to 'Map<Flat>::pathdistance(int [(((sizetype)(((ssizetype)n) + -1)) + 1)], int&, Base::Coord)'
45错误:调用“Map::path distance(int[((sizetype)((ssizetype)n+-1))+1)],int&,Base::Coord”时没有匹配的函数

我可以马上告诉你(甚至不用看你的源代码)最后一个参数
city\u location
的类型是
Base::Coordinate
,但是您的函数定义要求它是一个double。

这里有一个C语言的小程序,它处理旅行商问题。它基于分支定界算法使用res:堆栈和循环队列。出于测试目的,随机生成连接矩阵。出发城市为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;
}

请正确格式化您的代码好吗?这样会比较容易混淆。您可以像使用2D数组一样使用city_位置,但pathdistance需要一个常量。
int Path[n]你不能用变量作为数组的数量来声明数组。你的编译器允许它,但它是非标准的。如果你想要标准一致性,使用< C++ > STD::向量< /代码>。- 1可能是对其他问题的一个很好的答案,但是它只是与这个问题相关联的。为什么不呢?在Python中发布一个实现?因为问题是用C++标记的,并且是关于OP代码中的细节。
#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;
}
[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