다음을 통해 공유


컴파일러 오류 C2662

'function': 'this' 포인터를 'type1'에서 'type2'로 변환할 수 없습니다.

컴파일러에서 포인터를 .로 thistype1 변환할 type2수 없습니다.

이 오류는 개체에서 멤버가 아닌const 함수를 호출하여 발생할 수 있습니다 const . 가능한 해결 방법:

  • const 개체 선언에서 제거합니다.

  • 멤버 함수에 추가 const 합니다.

다음 샘플에서는 C2662를 생성합니다.

// C2662.cpp
class C {
public:
   void func1();
   void func2() const{}
} const c;

int main() {
   c.func1();   // C2662
   c.func2();   // OK
}

/clr을 사용하여 컴파일하는 경우 정규화된 관리 형식 또는 volatile 정규화된 형식에서 함수를 const 호출할 수 없습니다. 관리되는 클래스의 const 멤버 함수를 선언할 수 없으므로 const 관리 개체에서 메서드를 호출할 수 없습니다.

// C2662_b.cpp
// compile with: /c /clr
ref struct M {
   property M^ Type {
      M^ get() { return this; }
   }

   void operator=(const M %m) {
      M ^ prop = m.Type;   // C2662
   }
};

ref struct N {
   property N^ Type {
      N^ get() { return this; }
   }

   void operator=(N % n) {
      N ^ prop = n.Type;   // OK
   }
};

다음 샘플에서는 C2662를 생성합니다.

// C2662_c.cpp
// compile with: /c
// C2662 expected
typedef int ISXVD;
typedef unsigned char BYTE;

class LXBASE {
protected:
    BYTE *m_rgb;
};

class LXISXVD:LXBASE {
public:
   // Delete the following line to resolve.
   ISXVD *PMin() { return (ISXVD *)m_rgb; }

   ISXVD *PMin2() const { return (ISXVD *)m_rgb; };   // OK
};

void F(const LXISXVD *plxisxvd, int iDim) {
   ISXVD isxvd;
   // Delete the following line to resolve.
   isxvd = plxisxvd->PMin()[iDim];

   isxvd = plxisxvd->PMin2()[iDim];
}