Jump to content

Compilation Error: Pairs and xutility

- - - - -

  • Please log in to reply
5 replies to this topic

#1
thechef

thechef

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Hello, all! I am working on this program to find a solution to the traveling salesman problem. I am very frustrated because it doesn't seem to compile. I am getting this error:

error C2039: 'iterator_category' : is not a member of 'std::pair<_Ty1,_Ty2>'
It is in my "totalDis(...)" method. When I try to call "distance(...)" with two pairs from my vector, it tells me there are errors in xutility. There are a ton of errors, but the one above encapsulates them all. I am hoping the error is something obvious that I am missing.

Thank you for the help!

#include <fstream>
using std::ifstream;

#include <iostream>

#include <cmath>

#include <string>
using std::string;

#include <utility>
using std::make_pair;
using std::pair;

#include <vector>
using std::vector;

typedef pair< double, double > point;
typedef vector< point > graph;
typedef unsigned long ulong;

//----------------------------------------------------------

// pre: ifstream is a file containing the number of
//        coordinates to read in and the number of coordinates
// pst: returns vector of coordinate pairs
graph readFile( ifstream & );
// pre: graph is a vector of coordinate pairs
// pst: returns a vector of coordinate pairs, representing
//        the TSP path that visits each node exactly once
graph slowTSP( const graph & );
// pre: graph is a vector of coordinate pairs
// pst: returns a vector of coordinate pairs, representing
//        the TSP path that visits each node exactly once
graph fastTSP( const graph & );
// pst: returns the linear distance between two
//        coordinate points
const double distance( const point, const point );
// pst: returns the total linear distance of the cycle
//        that visits each point exactly once
const double totalDis( const graph & );

//----------------------------------------------------------

const string FILENAME( "input.txt" );

int main( int num, char **args )
{
    ifstream fin( FILENAME.c_str() );

    graph vec = readFile( fin );

    // print coordinates
    for (int i=0; i<vec.size(); ++i)
        std::cout << "(" << vec[i].first << ", "
                  << vec[i].second << ")\n";

    return 0;
}

//----------------------------------------------------------

graph readFile( ifstream & fin )
{
    graph v;
    ulong size;
    fin >> size;    // get number of coordinates

    double x;
    double y;
    for (int i=0; i<size; ++i) {
        // construct list of coordinate pairs
        fin >> x >> y;
        v.push_back( make_pair(x, y) );
    }

    return v;
}

//----------------------------------------------------------

graph slowTSP( const graph & in )
{
    graph min( in );

    return min;
}

//----------------------------------------------------------

graph fastTSP( const graph & in )
{
    graph v( in );

    return v;
}

//----------------------------------------------------------

const double distance( const point a, const point b )
{
    // a^2 + b^2 = c^2
    return (sqrt( pow((a.first - b.first), 2) +
                  pow((a.second - b.second), 2)));
}

//----------------------------------------------------------

const double totalDis( const graph & in )
{
    double dis = 0;
    // distance from start to end point
    for ( int i=0; i<in.size(); ++i) {
        double temp = distance(in.at(i-1), in.at(i));
        dis += temp;
    }
    // distance from end point back to start
    dis += distance( in.at(in.size()-1), in.at(0) );

    return dis;
}

//----------------------------------------------------------

I don't document code. If it was hard to write, it should be hard to read ;)

#2
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
you should pass the address...
It should be
double temp = distance(&in.at(i-1), &in.at(i));


#3
thechef

thechef

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Thank you so much! That error was really bothering me. Why do I need to pass the address? It totally worked, but I don't understand why. Thanks again!
I don't document code. If it was hard to write, it should be hard to read ;)

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
An iterator is very similar to a pointer. You often need to dereference both :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
thechef

thechef

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Hmm, interesting. I am still confused. lol Sorry, but I have a few more questions. How do iterators come into play here since I never declare an iterator? Also, the '&' should get the address of the object, so how can I pass an address of a pair to a function that just takes a regular pair, not a pointer to a pair? Sorry if these are silly questions; I am just trying to figure this out.

Thanks again!
I don't document code. If it was hard to write, it should be hard to read ;)

#6
thechef

thechef

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Oh! I figured it out...I think. One of my functions was named "distance", but there is also an STL function named "distance". It takes two iterators. My own distance function was just supposed to take pairs. I renamed my function to "dist", and the problem is solved. Thanks everybody!
I don't document code. If it was hard to write, it should be hard to read ;)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users