4

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&)'
1
  • 4
    How are you compiling it? It compiled just fine for me with the default settings. Commented Feb 10, 2014 at 22:31

1 Answer 1

6

When compiling C++ code you must use g++, not gcc.

Sign up to request clarification or add additional context in comments.

3 Comments

Alas I thought I was going crazy
I don't think that would cause these errors would it? It wouldn't understand the C++ syntax. This is a linker thing... Right?
@MooingDuck: IIUC the compiler is indeed the same, it's the linking phase that is c++ specific.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.