1

g++ コンパイラは次のことについて不平を言います:

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

このエラーの原因となっているコードは

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

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

コードを調べたところ、このエラーは によって返された参照型が原因であることがわかりましたgetOtherObj()。クラス定義で AddressSpace オブジェクトへの const 参照を返すようにします。

const AddressSpace &getAddressSpaceObj(){
   return addressSpace;
}

この定義を通常の参照を返すように変更した後、

AddressSpace &getAddressSpaceObj(){
    return addressSpace;
}

コンパイラはもう文句を言いません。なぜこのエラーがパラメータの不一致エラーとして宣言されているのだろうか? コンパイラがコンテンツを関数呼び出しのパラメーターとしてコピーせず、参照として渡したのはなぜですか?

4

1 に答える 1

5

resolve指定子がない場合はconst、参照でそれを呼び出すことができないconstため、それを non- に変更して動作させることと一致し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>
于 2013-03-15T17:22:40.763 に答える