constructors basic (reference to distance is ambiguous)

删除回忆录丶 提交于 2019-12-18 09:04:32

问题


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

回答1:


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




回答2:


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.




回答3:


This ambiguous errors occur due to using std functions as general functions. There are two ways to overcome this.

1) Instead of declaring using namespace std; use std::cout whenever you need to print any std::cin to take values and std::endl for new line.

#include <iostream>
#include <conio.h>

//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){
std::cout<<"Object have been initialized and assigned the values"<<std::endl;
meters=met;
centimeters=cen;

}
distance::distance(int met){
meters=met;
std::cout<<"One member has been initialized "<<std::endl;
std::cout<<"Please enter the distance in centimeters"<<std::endl;
std::cin>>centimeters;

}

void distance::display(){
std::cout<<"The distance in centimeters is "<<centimeters<<std::endl;
std::cout<<"The distance in meters is "<<meters<<std::endl;

}   
int main(){
//explicit call
distance a=distance(10,20);
a.display();
int c,m;
std::cout<<"Enter the distance in centimeters and meters"<<std::endl;
std::cin>>c>>m;

//implicit call
distance dist(c,m);
return 0;
}

2) You can also change the distance to Distance to not conflict with the inbuilt std::distance function.

#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;
}


来源:https://stackoverflow.com/questions/21079186/constructors-basic-reference-to-distance-is-ambiguous

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!