Dzung Nguyen
Dzung Nguyen

Reputation: 3944

Using of vector in C++

I'm having trouble with the following code and can't seem to figure out what is wrong

#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

double distance(int a, int b)
{
    return fabs(a-b);
}

int main()
{
    vector<int> age;
    age.push_back(10);
    age.push_back(15);

    cout<<distance(age[0],age[1]);
    return 0;
}

The error lies at calling of function distance.

/usr/include/c++/4.6/bits/stl_iterator_base_types.h: In instantiation of ‘std::iterator_traits<int>’:
test.cpp:18:30:   instantiated from here
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:166:53: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:167:53: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:168:53: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:169:53: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:170:53: error: ‘int’ is not a class, struct, or union type

Upvotes: 18

Views: 1739

Answers (4)

tmaric
tmaric

Reputation: 5477

You are trying to override std::distance function, try removing "using namespace std" and qualifying cout and endl with std::

#include <iostream>
#include <cmath>
#include <vector>


double distance(int a, int b)
{
    return fabs(a-b);
}

int main()
{
    std::vector<int> age;
    age.push_back(10);
    age.push_back(15);

    std::cout<< distance(age[0],age[1]);
    return 0;
}

The std::distance is used to count the number of elements in a container within a specified range. You can find more about it here.

Or you can rename your distance function if you want to introduce the std:: namespace:

#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

double mydistance(int a, int b)
{
    return fabs(a-b);
}

int main()
{
    vector<int> age;
    age.push_back(10);
    age.push_back(15);

    cout<<mydistance(age[0],age[1]);
    return 0;
}

This will make your code work, but it is not recommended to have "using namespace" declarations before definitions. When you write your code, you should avoid the second option, it's shown here only as an alternative for your code example.

Upvotes: 33

sehe
sehe

Reputation: 392999

Alternatively, you can use

 using foo::distance; // OR:
 using namespace foo;

 (distance)(x,y); // the (parens) prevent ADL

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258608

How about

cout<< ::distance(age[0],age[1]);

(other answers already suggest removing the using directive).

Upvotes: 9

Tony The Lion
Tony The Lion

Reputation: 63200

Don't use using namespace std when you're creating your own function called distance, because your call to distance is looking for std::distance and not your distance function.

You could also do this:

namespace foo
{
  double distance(int a, int b)
  {
    return fabs(a-b);
  }
}

int main()
{
   foo::distance(x,y); //now you're calling your own distance function.
}

Upvotes: 4

Related Questions