-5

The code is below. The code does not compile on an online compiler, and I have no idea why. It is short and pretty self-explanatory, please look below for details.

#include <iostream>
#include <cmath> 

using namespace std;
int N;

int distance(int a, int b){
    if(abs(a-b) > N/2){
        return N - abs(a-b);
    }
    return abs(a-b);
}

bool test(int x, int y){
    if(distance(x,y) <=2){
        return true;
    }
    return false;
}

int main()
{
    N = 2;
   cout << "Hello World" << endl; 
   cout << test(3,4) << endl;
   return 0;
}

Error message below:

In file included from /usr/include/c++/4.8.3/bits/stl_algobase.h:65:0,                           
                 from /usr/include/c++/4.8.3/bits/char_traits.h:39,                              
                 from /usr/include/c++/4.8.3/ios:40,                                             
                 from /usr/include/c++/4.8.3/ostream:38,                                         
                 from /usr/include/c++/4.8.3/iostream:39,                                        
                 from main.cpp:1:                                                                
/usr/include/c++/4.8.3/bits/stl_iterator_base_types.h: In instantiation of 'struct std::iterator_
traits<int>':                                                                                    
/usr/include/c++/4.8.3/bits/stl_iterator_base_funcs.h:114:5:   required by substitution of 'templ
ate<class _InputIterator> typename std::iterator_traits<_Iterator>::difference_type std::distance
(_InputIterator, _InputIterator) [with _InputIterator = int]'                                    
main.cpp:15:20:   required from here                                                             
/usr/include/c++/4.8.3/bits/stl_iterator_base_types.h:165:53: error: 'int' is not a class, struct
, or union type                                                                                  
       typedef typename _Iterator::iterator_category iterator_category;                          
                                                     ^                                           
/usr/include/c++/4.8.3/bits/stl_iterator_base_types.h:166:53: error: 'int' is not a class, struct
, or union type                                                                                  
       typedef typename _Iterator::value_type        value_type;                                 
                                                     ^                                           
/usr/include/c++/4.8.3/bits/stl_iterator_base_types.h:167:53: error: 'int' is not a class, struct
, or union type                                                                                  
       typedef typename _Iterator::difference_type   difference_type;                            
                                                     ^                                           
/usr/include/c++/4.8.3/bits/stl_iterator_base_types.h:168:53: error: 'int' is not a class, struct
, or union type                                                                                  
       typedef typename _Iterator::pointer           pointer;                                    
                                                     ^                                           
/usr/include/c++/4.8.3/bits/stl_iterator_base_types.h:169:53: error: 'int' is not a class, struct
, or union type                                                                                  
       typedef typename _Iterator::reference         reference;
abelenky
  • 63,815
  • 23
  • 109
  • 159
user17902
  • 45
  • 3

1 Answers1

7
using namespace std;

This is a bad idea; it dumps anything that's been declared in the std namespace into the global namespace, where it might conflict with anything you declare in the global namespace.

int distance(int a, int b)

This declares a function in the global namespace that conflicts with a function template of the same name in the std namespace.

if(distance(x,y) <=2)

The std::distance template is a better match than your function, according to the arcane rules of overload resolution. Trying to instantiate that, it fails since it can only be instantiated for iterator types, not int.

The best option is to remove the rogue using-directive, and add std:: to anything you use from the standard library. If you don't want to do that for some reason, then qualify your function call to specify the one declared in the global namespace:

if(::distance(x,y) <=2)
Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • It works. Thanks for the help and the clear explanation. – user17902 Dec 15 '14 at 17:13
  • @user17902 Welcome on SO! Great that you got your problem sorted. Please consider upvoting all useful answers and accepting the best (should be easy with only one answer). – Walter Dec 15 '14 at 17:18