2013-03-15 2 views
1

그램 ++ 컴파일러 불평 :C++ : 일치 함수 호출, 특히이 불일치의 파라미터 타입으로서 선언되지

error: no matching function for call to ‘AddressSpace::resolve(ClassOne&, ClassTwo*&, ClassThree&) const’ 
note: candidates are: bool AddressSpace::resolve(ClassOne&, ClassTwo*, ClassThreer) <near match> 

이 오류 원인 코드는 I 코드로 팠 발견

void Class::handler(ClassOne& objOne, ClassTwo& objTwo, 
     ClassThreer objThree) { 

    obj.getAddressSpaceObj().resolve(objOne, objTwo.pointer, objThree); 
} 

인 이 오류는 getOtherObj()에 의해 반환 된 참조 형식 때문에 발생합니다. 나는 컴파일러에 대해 불평하지 않는,

AddressSpace &getAddressSpaceObj(){ 
    return addressSpace; 
} 

를, 그것은 클래스 정의의 주소 공간 객체에 const를 참조를 반환 할 수 있도록 내가 보통의 참조를 반환하기 위해이 정의를 변경 한 후

const AddressSpace &getAddressSpaceObj(){ 
    return addressSpace; 
} 

참조 더 이상. 이 오류가 매개 변수가 일치하지 않는 오류로 선언 된 이유는 무엇입니까? 컴파일러가 컨텐츠 호출의 매개 변수로 컨텐츠를 복사하지 않고 참조로 전달한 이유는 무엇입니까? 그게 비 const되는 것으로 변경하고 지금 일을 가진과 일치 될 수 있도록 resolve하면

답변

5

은, 당신이 const 기준에 호출 할 수있는 const 지정이 없습니다. 여기 정말 사소한 예입니다

#include <iostream> 

class A 
{ 
    public: 
     void someFuncA() {}; 
     void someFuncB() const {} ; 
} ; 

int main() 
{ 
    A 
    a1 ; 
    const A &aRef = a1 ; 

    a1.someFuncA() ; 

    // Below won't work because aRef is a const & but someFuncA() not const 
    //aRef.someFuncA() ; 

    // Below will work since someFuncB() is const 
    aRef.someFuncB() ; 
} 

그냥 완전성을 위해, 당신은 aRef.someFuncA() 주석 다음받을 오류는 다음과 유사 할 경우 :

19:19: error: no matching function for call to 'A::someFuncA() const' 
19:19: note: candidate is: 
6:12: note: void A::someFuncA() <near match> 
+0

아주 많은 감사합니다!. – byteocean