I am trying to complete my assignment.which says In a Distance class create an overloaded * operator so that two distances can be multiplied together. Here is my program:
#include<iostream>
using namespace std;
class distance
{
private:
int a;
public:
distance():a(10){}
distance(int x)
{
a=x;
}
void print()
{
cout<<"\n a = "<<a<<" \n ";
}
distance operator *(distance);
};
distance distance :: operator *(distance d)
{
a=a*d.a;
return distance(a);
}
int main()
{
distance d1,d2,d3;
d1.print();
d2.print();
d3.print();
d3=d1 * d2;
d3.print();
return 0;
}
But I get a compilation error saying:
'distance' : ambiguous symbol.
when I run it on VC++
But it runs fine on Turbo C (by minor change: #include<iostream.h>).
Please explain where am I getting wrong. Thanks in advance.
There’s already a function called
std::distance, which you’re bringing into scope throughusing namespace stdand which clashes with yourdistanceclass.Call your class something else (e.g.
Distance) or, better yet, removeusing namespace std.P.S. This is a nice illustration of why
using namespace Xin this manner usually isn’t a good idea.