0

While compiling the following code i am getting the error as

reference to 'distance' is ambiguous

    #include<iostream>
using namespace std;
class distance
{
    int feet,inches;
    distance():feet(0),inches(0)
    {

    }
    distance(int f,int i):feet(f),inches(i)
    {
    }

    void show()

    {
        cout<<"feet  "<<feet;
        cout<<endl<<"inches   "<<inches;
    }

    distance operator + (distance) ;
};

distance distance::operator + (distance d)
{
    int f,i;
    f=feet+d.feet;
    i=inches+d.inches;
    return distance(f,i);
}

int main()
{
    distance d1;
    distance d2(2,3),d3(7,5);;
d1=d2+d3;
d1.show();
}

can anyone help me with the error. And provide me the solution and as to why i am getting this error.

Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
Ashish
  • 27
  • 1
  • 2
  • 7

2 Answers2

14

And this is why using namespace std; should not be used. Your class distance is clashing with the standard function std::distance. Get rid of the using namespace std; and if you are going to be using a standard component use std::name_of_thing every time you use it or you can use using std::name_of_thing.

Community
  • 1
  • 1
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
1

Your class name is clashing with another symbol from the namespace, changing your class name to something else like Distance would be one possible solution.

shafeen
  • 2,431
  • 18
  • 23