C++ undefined reference using fstream
c++
Solution
When compiling C++ code you must use g++, not gcc.
Problem
So I had to make quick code in just a few minutes for a TSP type problem, but when I tried to read in the file, my compiler freaked out. Here is the sample code, what causes this error? ``` #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; } ``` Sample error: ``` 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&)' ```