編譯器錯誤 C2662
'function' : 無法將 'this' 指標從 'type1' 轉換為 'type2'
編譯程式無法將 this
指標從 type1
轉換為 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
限定的Managed型別上const
呼叫函式。 您無法宣告Managed類別的 const 成員函式,因此您無法在 const Managed 物件上呼叫方法。
// 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];
}