2014-01-12 3 views
2

저는 생성자를 연습하고있었습니다. 아래 코드는 연습했지만 오류가 발생했습니다 "거리에 대한 참조가 모호합니다."나는 내 오류를 식별하지 못했습니다. 제발 도와주세요. 나는 그것을 해왔다.기본 생성자 (거리에 대한 참조가 모호합니다)

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

표시되는대로 오류를 표시 할 수 있습니까? (복사 및 붙여 넣기를 의미) – 0x499602D2

+0

\ main.cpp | 45 | 오류 : 'distance'에 대한 참조가 모호합니다. – user3188072

+0

@ user3188072 질문을 오류로 업데이트하십시오. –

답변

8

중지 한 distancestd::distance와 충돌

using namespace std; 

하고.

빠른/더러운 수정 ::distance와 주에서 distance을 모두 교체하고,보다 강력한 수정은 모든 표준 라이브러리 호출에 std::을 추가하고 using namespace std; 제거합니다.

1

표준 C++ 네임 스페이스 std ::는 이미 함수를 선언 할 때 이미 이름 거리를 사용하고 있습니다. 지시문 using namespace std;을 지정하면이 표준 이름이 선언 영역, 즉 전역 이름 공간에 도입되었습니다.

함수를 주 접두사로 사용하려면 두 콜론으로 모든 거리 표시를 접두사로 사용하십시오. 예를 들어,

::distance a=::distance(10,20); 

등등.

관련 문제