c++ windows error C2662 C2663

37 篇文章 0 订阅
16 篇文章 0 订阅


//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];  
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: CMake 是一个跨平台的构建系统,它可以帮助您在 Windows 和其他操作系统之间轻松地创建和维护项目。 如果您在 Windows 上使用 CMake 遇到了 error C2398 错误,那么可以使用以下方法来忽略这个错误: 1. 在 CMakeLists.txt 文件中使用 add_compile_options() 命令添加编译选项 /wd2398。 2. 使用 #pragma warning(disable: 2398) 来忽略特定的警告。 3. 在 Visual Studio 中打开项目属性页,然后在 "C/C++" - "常规" 选项卡中添加 /wd2398 到 "附加包含目录" 中。 注意,忽略错误可能会导致程序运行不正常,应该尽量避免这种做法,而是尝试解决问题的根源。 ### 回答2: CMake是一个跨平台的开源构建工具,用于自动生成编译过程的构建文件。对于CMake在Windows环境下出现的"error C2398"错误,我们可以通过以下方式忽略或解决: 1. 忽略警告:可以通过在CMakeLists.txt文件中添加相应的编译选项来忽略警告。例如,对于MSVC编译器,我们可以使用以下命令来忽略该错误: ```cmake if(MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd2398") endif() ``` 这将在生成的构建文件中添加编译参数"/wd2398",告知编译器忽略指定的错误。 2. 修复错误:如果忽略警告不是你希望的解决方案,你也可以尝试修复错误。"error C2398"通常是由于不兼容的函数或构造函数使用而导致的,这可能是由于代码的更新或编译器的更新引入的。要修复错误,请查看代码中报错的位置,并根据报错信息进行相应的修改。 总之,CMake在Windows环境下忽略"error C2398"错误的方法是使用相应的编译选项来忽略或修复错误。如果你希望忽略该错误,可以在CMakeLists.txt文件中添加相应的编译选项来禁用该警告。但是,请注意在决定是否忽略警告时,需要考虑代码的稳定性和可靠性。如果可以的话,最好是修复错误以确保代码的正确性。 ### 回答3: 在CMake中,当我们在Windows环境编译项目时,遇到error C2398错误时,可以采取一些措施来忽略这个错误。 首先,要了解error C2398是指C语言中的一个编译错误,它表示在C++编译器中对类模板进行了错误的特化尝试。当CMake在Windows环境下编译时,可能会出现这个错误,我们可以通过以下方法来解决。 一种方法是在CMakeLists.txt文件中添加额外的编译选项来忽略error C2398。在编译选项中,我们可以使用"/we"参数来忽略指定的警告信息。我们可以将"/we(N)"添加到CMakeLists.txt文件中,其中(N)表示error C2398的警告码。例如,在CMakeLists.txt文件中添加以下内容可以忽略error C2398错误: ```cmake if(MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /we<warning_number>") endif() ``` 另一种方法是使用预编译头文件来忽略错误。我们可以在CMakeLists.txt文件中添加预编译头文件(pch.h),并在源文件中包含这个预编译头文件。预编译头文件中可以包含一些命令,用于禁用或忽略指定的警告。例如,在pch.h文件中添加以下命令可以忽略error C2398错误: ```cpp #pragma warning(disable : <warning_number>) ``` 然后,在源文件中包含这个预编译头文件: ```cpp #include "pch.h" ``` 总之,在CMake中忽略error C2398错误的方法有很多种。我们可以通过添加编译选项或使用预编译头文件来忽略这个错误。根据具体项目的需求和编译器的要求,选择合适的方法来解决这个问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值