c++ windows error C2662 C2663


//z 2014-08-12 12:39:50 L.141'40810 BG57IV3@XCL T961939060 .K.F37272252  [T14,L639,R15,V232]
VC C++ windows error C2662 C2663

在const成员函数中调用如下语句时:
CBitmap *pOldBitmap = m_dc.SelectObject(&bmpIcon);

出现了以下错误:
error C2663: “CDC::SelectObject”: 7 个重载没有“this”指针的合法转换
其他语句也出现了下列错误:
error C2662: “CDC::SetStretchBltMode”: 不能将“this”指针从“const CDC”转换为“CDC &”
转换丢失限定符
error C2662: “CDC::StretchBlt”: 不能将“this”指针从“const CDC”转换为“CDC &”
转换丢失限定符
error C2663: “CDC::SelectObject”: 7 个重载没有“this”指针的合法转换
//z 2014-08-12 12:39:50 L.141'40810 BG57IV3@XCL T961939060 .K.F37272252  [T14,L639,R15,V232]

msdn 上 vc2013 的说明也比较清楚。还有例子。
c2663
tion' : number overloads have no legal conversions for 'this' pointer

The compiler could not convert this to any of the overloaded versions of the member function.

This error can be caused by invoking a non-const member function on a const object. Possible resolutions:

  1. Remove the const from the object declaration.

  2. Add const to one of the member function overloads.

The following sample generates C2663:

// C2663.cpp
struct C {
   void f() volatile {}
   void f() {}
};

struct D {
   void f() volatile;
   void f() const {}
};

const C *pcc;
const D *pcd;

int main() {
   pcc->f();    // C2663
   pcd->f();    // OK
}


C2662
'function' : cannot convert 'this' pointer from 'type1' to 'type2'

The compiler could not convert the this pointer from type1to type2.

This error can be caused by invoking a non-const member function on a const object. Possible resolutions:

  • Remove the const from the object declaration.

  • Add const to the member function.

The following sample generates C2662:

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

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

When compiling with /clr, you cannot call a function on a const or volatile qualified managed type. You cannot declare a const member function of a managed class, so you cannot call methods on const managed objects.

// 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
   }
};

The following sample generates 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];  
}
posted @ 2014-08-12 12:39  BiG5  阅读(454)  评论(0编辑  收藏  举报