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

Categories

0 votes
654 views
in Technique[技术] by (71.8m points)

c++ - constructors basic (reference to distance is ambiguous)

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

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

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...