编译器错误 C2662

来源:互联网 发布:专业手机维修软件 编辑:程序博客网 时间:2024/06/06 12:52

编译器错误 C2662

Visual Studio 2005
其他版本
1(共 1)对本文的评价是有帮助 评价此主题

错误消息

“function”: 不能将“this”指针从“type1”转换为“type2”

编译器不能将 this 指针从 type1 转换为 type2

此错误可能是由对 const 对象调用非 const 成员函数引起的。可能的解决方案:

  • 从对象声明中移除 const

  • 将 const 添加到成员函数中。

下面的示例生成 C2662:

// C2662.cppclass C {public:   void func1();   void func2() const{}} const c;int main() {   c.func1();   // C2662   c.func2();   // OK}

在使用 /clr 编译时,不能对 const 或 volatile 限定托管类型调用函数。由于不能声明托管类的常量成员函数,因此不能调用常量托管对象上的方法。

// C2662_b.cpp// compile with: /c /clrref 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 expectedtypedef 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];  }

0 0
原创粉丝点击