Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
643 views
Welcome To Ask or Share your Answers For Others

1 Answer

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;.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...