I was practicing constructors. Below is the code which I was practicing but got an error that "reference to distance is ambiguous" I could not identify my error, Please help me. I have been trying that .
#include <iostream>
#include <conio.h>
using namespace std;
//distance
class distance
{
public:
distance(int met, int cen);
distance(int met);
void display();
private:
int meters;
int centimeters;
};
distance::distance(int met, int cen){
cout<<"Object have been initialized and assigned the values"<<endl;
meters=met;
centimeters=cen;
}
distance::distance(int met){
meters=met;
cout<<"One member has been initialized "<<endl;
cout<<"Please enter the distance in centimeters"<<endl;
cin>>centimeters;
}
void distance::display(){
cout<<"The distance in centimeters is "<<centimeters<<endl;
cout<<"The distance in meters is "<<meters<<endl;
}
int main(){
//explicit call
distance a=distance(10,20);
a.display();
int c,m;
cout<<"Enter the distance in centimeters and meters"<<endl;
cin>>c>>m;
//implicit call
distance dist(c,m);
return 0;
}
stop doing
using namespace std;
your distance is conflicting with std::distance.
A quick/dirty fix is replace all your distance in main with ::distance, a more robust fix is to add std:: to all standard library call and get rid of using namespace std;.
The problem is that standard C++ namespace std:: uses already name distance for declaring a function. As you specified directive using namespace std; then you introduced this standard name in your declarative region i.e. in the global namespace.
To make the program to work in function main prefix all occurences of distance with two colons. For example
::distance a=::distance(10,20);
and so on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With