Strange compiling error.

I wrote this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <vector>
#include <cmath>
#include <climits>
#include <algorithm>
#include <iomanip>
#include <utility>

using namespace std;

vector<vector<double> > mreza;
vector<pair<int,int> > dots;

double distance(pair<int,int> one,pair<int,int> two){
	double tmp;
	tmp=sqrt(abs(one.first-two.first)*abs(one.first-two.first)+abs(one.second-two.second)*abs(one.second-two.second));
	return tmp;
}
const double inf=ULONG_MAX;

int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		int x,y;
		cin>>x>>y;
		dots.push_back(make_pair(x,y));
	}
	vector<double> temp(n,inf);
	mreza.insert(mreza.begin(),n,temp);
	for(int x=0;x<n;x++)
		for(int y=0;y<n;y++){
			if(y==x) continue;
			mreza[x][y]=mreza[y][x]=distance(dots[x],dots[y]);
		}

	vector<double> dist(n,inf);
	vector<bool> bio(n,false);
	dist[0]=0;
	for(int x=0;x<n-1;x++){
		double udaljenost=inf+1;
		int elem;
		for(int y=0;y<n;y++){
			if(udaljenost>dist[y] && !bio[y]){
				udaljenost=dist[y];
				elem=y;
			}
		}
		bio[elem]=true;
		for(int y=0;y<n;y++){
			if(dist[y]>udaljenost+mreza[elem][y]);
				dist[y]=udaljenost+mreza[elem][y];
		}
	}

	double max=0;
	for(int x=0;x<n;x++){
		if(dist[x]>max) max=dist[x];
	}
	cout<<setprecision(2)<<max;
	return 0;
}


I tried to compile it, and I got this error:
1
2
3
4
5
6
7
/home/capttawish/Dropbox/cpp/natjecanja/2011/mreza.cpp:34|52|instantiated from here|
/usr/include/c++/4.6/bits/stl_iterator_base_types.h|166|error: no type named ‘iterator_category’ in ‘struct std::pair<int, int>’|
/usr/include/c++/4.6/bits/stl_iterator_base_types.h|167|error: no type named ‘value_type’ in ‘struct std::pair<int, int>’|
/usr/include/c++/4.6/bits/stl_iterator_base_types.h|168|error: no type named ‘difference_type’ in ‘struct std::pair<int, int>’|
/usr/include/c++/4.6/bits/stl_iterator_base_types.h|169|error: no type named ‘pointer’ in ‘struct std::pair<int, int>’|
/usr/include/c++/4.6/bits/stl_iterator_base_types.h|170|error: no type named ‘reference’ in ‘struct std::pair<int, int>’|
||=== Build finished: 5 errors, 0 warnings ===|

I use Codeblocks with g++.
The code compiles and runs for me.
Thanks.
I tried to reinstall g++ but it still shows the same error.
The code should compile without errors.

As far as I can make out, there seems to be a bug wrt SFINAE in the version of g++ that you are using.
std::distance is declared thus:
1
2
template< typename ITERATOR >
typename std::iterator_traits<ITERATOR>::difference_type std::distance( ITERATOR,ITERATOR ) ;


There is a work-around; disable lookup of std::distance() with a scope resolution operator:
1
2
//mreza[x][y]=mreza[y][x]=distance(dots[x],dots[y]);
mreza[x][y]=mreza[y][x] = ::distance( dots[x], dots[y] ) ;
Last edited on
Thanks for help. I solved the problem with adding a scope resolution operator as suggested by @JLBorges.
That is because instead of using your distance function it is trying to instantiate the distance function in the from the STL (which is why typing "using namespace std;" can flood your namespace). You can fix this by creating your own namespace for that function if you like.

e.g.
1
2
3
4
5
6
7
8
9
10
11
namespace myNamespace{
double distance(pair<int,int> one,pair<int,int> two){
	double tmp;
	tmp=sqrt(abs(one.first-two.first)*abs(one.first-two.first)+abs(one.second-two.second)*abs(one.second-two.second));
	return tmp;
}
}

//... Then when you use your distance fucntion..
mreza[x][y]=mreza[y][x]=myNamespace::distance(dots[x],dots[y]);
> That is because instead of using your distance function it is trying to instantiate the distance function in the from the STL
> (which is why typing "using namespace std;" can flood your namespace).

Actually, not having using namespace std ; would make absolutely no difference in this particular case. Koening lookup (aka argument-dependent lookup or ADL ) would result in namespace std being looked up any way.
http://en.wikipedia.org/wiki/Argument-dependent_name_lookup

Very interesting read, thanks for that.
Topic archived. No new replies allowed.