所以我需要在几分钟内快速编写代码来解决TSP问题,但是当我尝试读取文件时,我的编译器出现了错误。以下是示例代码,是什么导致了这个错误?
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int tour_cost (int start, int end, int* array, int* tour) //calculate tour cost
{
int cost = 0;
for (int i = start; i < end; i++)
cost += array[tour[i] * tour[i] + tour[i + 1]];
return cost;
}
int main()
{
int array [625];
int tour [25];
int dist = 0;
ifstream infile ("cities.txt");
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 25; j++ ) {
infile >> dist;
array[i*i + j] = dist;
}
tour[i] = i;
}
cout << tour_cost (0,24,array,tour);
return 0;
}
样本错误:
ps2.cpp:(.text+0xc0): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(char const*, std::_Ios_Openmode)'
ps2.cpp:(.text+0xe8): undefined reference to `std::istream::operator>>(int&)'