warning C4047: 'initializing' : 'char *' differs in levels of indirection from 'const int '

本文解析了MSDN中关于编译器警告C4047的详细内容,该警告提示操作符作用于不同间接寻址级别的标识符。通过两个示例代码片段展示了如何触发这一警告,以及如何正确匹配指针类型以避免此类警告。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下来自MSDN

编译器警告(等级 1)C4047

错误消息

“operator”:“identifier1”与“identifier2”的间接寻址级别不同

指针可指向变量(一级间接寻址),指向另一个指向变量的指针(二级间接寻址)等。

示例

下面的示例生成 C4047:

// C4047.c
// compile with: /W1

int main() {
   char **p = 0;   // two levels of indirection
   char *q = 0;   // one level of indirection

   char *p2 = 0;   // one level of indirection
   char *q2 = 0;   // one level of indirection

   p = q;   // C4047
   p2 = q2;
}

下面的示例生成 C4047:

// C4047b.c
// compile with: /W1
#include 

int main() {
   int i;
   FILE *myFile = NULL;
   errno_t  err = 0;
   char file_name[256];
   char *cs = 0;

   err = fopen_s(&myFile, "C4047.txt", "r");
   if ((err != 0) || (myFile)) {
      printf_s("fopen_s failed!/n");
      exit(-1);
    }
   i = fgets(file_name, 256, myFile);   // C4047
   cs = fgets(file_name, 256, myFile);   // OK

}

D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In member function 'void INT::normalize()': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 27 50 [错误] no matching function for call to 'INT::abs(__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 366 13 [说明] candidate: 'INT INT::abs() const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 366 13 [说明] candidate expects 0 arguments, 1 provided D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In member function 'INT INT::operator-() const': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 121 28 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 9 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 9 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 134 36 [说明] initializing argument 1 of 'bool INT::operator!=(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In member function 'INT INT::operator/(const INT&) const': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 230 30 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 9 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 9 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 126 36 [说明] initializing argument 1 of 'bool INT::operator==(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 231 50 [错误] call of overloaded 'INT(int)' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 9 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 108 9 [说明] candidate: 'INT::INT(const string&)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 9 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 13 7 [说明] candidate: 'INT::INT(const INT&)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 13 7 [说明] candidate: 'INT::INT(INT&&)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In member function 'INT INT::operator>>(int) const': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 283 36 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 9 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 9 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 126 36 [说明] initializing argument 1 of 'bool INT::operator==(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 286 47 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 9 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 9 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 134 36 [说明] initializing argument 1 of 'bool INT::operator!=(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In member function 'std::string INT::to_binary_string() const': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 374 30 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 9 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 9 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 126 36 [说明] initializing argument 1 of 'bool INT::operator==(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 378 29 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 9 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 9 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 134 36 [说明] initializing argument 1 of 'bool INT::operator!=(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 379 41 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 9 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 9 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 126 36 [说明] initializing argument 1 of 'bool INT::operator==(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In static member function 'static INT INT::from_binary_string(const string&)': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 387 26 [错误] call of overloaded 'INT(int)' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 9 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 108 9 [说明] candidate: 'INT::INT(const string&)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 9 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 13 7 [说明] candidate: 'INT::INT(const INT&)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 13 7 [说明] candidate: 'INT::INT(INT&&)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In member function 'std::string INT::to_string() const': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 399 30 [错误] aggregate 'std::stringstream ss' has incomplete type and cannot be defined D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In function 'INT pow(INT, INT)': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 423 27 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 9 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 9 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 136 35 [说明] initializing argument 1 of 'bool INT::operator<(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 423 37 [错误] conversion from 'int' to 'INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 9 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 9 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 425 30 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 9 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 9 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 152 35 [说明] initializing argument 1 of 'bool INT::operator>(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In function 'INT gcd(INT, INT)': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 439 21 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 9 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 9 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 134 36 [说明] initializing argument 1 of 'bool INT::operator!=(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In function 'INT factorial(const INT&)': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 452 17 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 9 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 9 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 136 35 [说明] initializing argument 1 of 'bool INT::operator<(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 453 18 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 9 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 9 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 126 36 [说明] initializing argument 1 of 'bool INT::operator==(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In member function 'void INT::normalize()': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 27 22 [错误] no matching function for call to 'INT::abs(__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 366 6 [说明] candidate: 'INT INT::abs() const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 366 6 [说明] candidate expects 0 arguments, 1 provided D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In member function 'INT INT::operator-() const': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 121 14 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 2 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 2 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 134 29 [说明] initializing argument 1 of 'bool INT::operator!=(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In member function 'INT INT::operator/(const INT&) const': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 230 16 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 2 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 2 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 126 29 [说明] initializing argument 1 of 'bool INT::operator==(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 231 36 [错误] call of overloaded 'INT(int)' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 2 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 108 2 [说明] candidate: 'INT::INT(const string&)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 2 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 13 7 [说明] candidate: 'INT::INT(const INT&)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 13 7 [说明] candidate: 'INT::INT(INT&&)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In member function 'INT INT::operator>>(int) const': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 283 15 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 2 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 2 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 126 29 [说明] initializing argument 1 of 'bool INT::operator==(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 286 26 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 2 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 2 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 134 29 [说明] initializing argument 1 of 'bool INT::operator!=(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In member function 'std::string INT::to_binary_string() const': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 374 16 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 2 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 2 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 126 29 [说明] initializing argument 1 of 'bool INT::operator==(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 378 15 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 2 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 2 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 134 29 [说明] initializing argument 1 of 'bool INT::operator!=(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 379 20 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 2 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 2 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 126 29 [说明] initializing argument 1 of 'bool INT::operator==(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In static member function 'static INT INT::from_binary_string(const string&)': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 387 12 [错误] call of overloaded 'INT(int)' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 2 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 108 2 [说明] candidate: 'INT::INT(const string&)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 2 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 13 7 [说明] candidate: 'INT::INT(const INT&)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 13 7 [说明] candidate: 'INT::INT(INT&&)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In member function 'std::string INT::to_string() const': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 399 16 [错误] aggregate 'std::stringstream ss' has incomplete type and cannot be defined D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In function 'INT pow(INT, INT)': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 423 13 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 2 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 2 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 136 28 [说明] initializing argument 1 of 'bool INT::operator<(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 423 23 [错误] conversion from 'int' to 'INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 2 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 2 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 425 16 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 2 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 2 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 152 28 [说明] initializing argument 1 of 'bool INT::operator>(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In function 'INT gcd(INT, INT)': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 439 14 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 2 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 2 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 134 29 [说明] initializing argument 1 of 'bool INT::operator!=(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 0 -1 In function 'INT factorial(const INT&)': D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 452 10 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 2 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 2 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 136 28 [说明] initializing argument 1 of 'bool INT::operator<(const INT&) const' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 453 11 [错误] conversion from 'int' to 'const INT' is ambiguous D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 112 2 [说明] candidate: 'INT::INT(const char*)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 90 2 [说明] candidate: 'INT::INT(long long int)' D:/HuaweiMoveData/Users/94197/Documents/untitled1.cpp 126 29 [说明] initializing argument 1 of 'bool INT::operator==(const INT&) const' 、
最新发布
07-14
--------------------Configuration: My - Win32 Debug-------------------- Compiling resources... Compiling... StdAfx.cpp Compiling... My.cpp MainFrm.cpp MyDoc.cpp MyView.cpp F:\My\MyView.cpp(59) : error C2065: 'dotMatrix_Liu' : undeclared identifier F:\My\MyView.cpp(59) : error C2109: subscript requires array or pointer type F:\My\MyView.cpp(59) : error C2109: subscript requires array or pointer type F:\My\MyView.cpp(108) : error C2039: 'DrawVectorName' : is not a member of 'CMyView' f:\my\myview.h(13) : see declaration of 'CMyView' F:\My\MyView.cpp(116) : error C2084: function 'const struct AFX_MSGMAP *__stdcall CMyView::_GetBaseMessageMap(void)' already has a body F:\My\MyView.cpp(116) : error C2084: function 'const struct AFX_MSGMAP *__thiscall CMyView::GetMessageMap(void) const' already has a body F:\My\MyView.cpp(116) : error C2374: 'protected: static struct AFX_MSGMAP const CMyView::messageMap' : redefinition; multiple initialization f:\my\myview.h(54) : see declaration of 'protected: static struct AFX_MSGMAP const CMyView::messageMap' F:\My\MyView.cpp(116) : error C2374: 'private: static struct AFX_MSGMAP_ENTRY const * const CMyView::_messageEntries' : redefinition; multiple initialization f:\my\myview.h(54) : see declaration of 'private: static struct AFX_MSGMAP_ENTRY const * const CMyView::_messageEntries' F:\My\MyView.cpp(118) : error C2509: 'OnKeyDown' : member function not declared in 'CMyView' f:\my\myview.h(13) : see declaration of 'CMyView' F:\My\MyView.cpp(123) : error C2040: 'dotMatrix_Liu' : 'unsigned char [16][16]' differs in levels of indirection from 'int' Generating Code... 执行 cl.exe 时出错. My.exe - 1 error(s), 0 warning(s) 怎么解决
03-19
<think>嗯,用户在使用MFC项目时遇到了MyView.cpp文件中的多个编译错误,包括C2065、C2109、C2039等。首先,我需要根据用户提供的引用内容来分析可能的原因。引用[1]提到error C2661,是因为new操作符没有接受3个参数的重载函数,而引用[2]中的代码示例展示了如何处理构造函数中的异常,并初始化图形驱动。 首先,C2065是未声明的标识符错误,可能是由于头文件未包含或者命名空间的问题。比如,用户可能在代码中使用了一个类或变量,但没有包含相应的头文件,或者没有使用正确的命名空间。例如,如果用户使用了OpenGl_GraphicDriver但没有包含相关的头文件,就会报这个错误。需要检查所有未声明的标识符,确保对应的头文件已经包含,并且使用了正确的命名空间,比如可能需要的OpenCASCADE库中的命名空间。 接下来是C2109错误,数组类型下标的问题。这可能是因为用户尝试对非数组类型的变量使用下标访问,或者数组的维度不正确。例如,如果用户声明了一个指针但没有分配内存,就使用下标访问,会导致这个错误。需要检查代码中的数组和指针使用,确保它们被正确初始化和分配了足够的内存,或者确认变量确实是数组类型。 然后是C2039错误,成员函数不存在。这说明用户调用了一个类中不存在的成员函数。可能的原因包括头文件未更新、函数名拼写错误,或者函数所在的类未被正确继承。例如,用户可能在MyView类中调用了一个函数,但该函数并没有在类定义中声明,或者基类中没有这个虚函数。需要检查类的定义,确认成员函数是否存在,拼写是否正确,以及是否在头文件中声明。 关于重复定义错误,通常是因为头文件中的变量或函数被多次包含,导致重复定义。需要检查是否在头文件中使用了#pragma once或者#ifndef/#define/#endif来防止重复包含。同时,确保变量和函数在头文件中声明为extern,而在源文件中定义。 此外,用户提供的引用[2]中的代码示例可能涉及到异常处理和资源初始化的问题。如果在初始化图形驱动时出现异常,可能需要确保所有依赖的库和头文件正确配置,并且链接了正确的库文件。例如,OpenCASCADE库的相关头文件路径和库路径是否在项目设置中正确配置,是否链接了必要的lib文件。 还需要检查项目的属性设置,包括包含目录、库目录、预处理器定义等是否正确。特别是使用第三方库如OpenCASCADE时,这些设置错误常常会导致编译错误。另外,代码中的语法错误,比如缺少分号、括号不匹配等,也可能引发其他错误,需要仔细检查代码结构。 最后,建议用户逐步排查错误,从第一个报错开始解决,因为后面的错误可能是由前面的错误引起的。例如,未声明的标识符可能导致后续的语法分析错误。同时,确保所有使用的类和函数在项目中正确引入,并且版本兼容。</think>在MFC项目中,`MyView.cpp`文件出现多种编译错误的原因及解决方案如下: --- ### 一、错误类型分析 1. **error C2065(未声明的标识符)** - **原因**:使用了未包含头文件或未定义的类型/变量,例如`OpenGl_GraphicDriver`未声明[^2]。 - **解决**: - 检查是否包含相关头文件(如`#include <OpenGl_GraphicDriver.hxx>`)。 - 确认命名空间是否正确(如`using namespace OpenGl`)。 2. **error C2109(数组类型下标问题)** - **原因**:对非数组变量使用下标操作,或数组维度不匹配。 - **解决**: - 检查变量是否为数组类型,例如`int* arr`需确保已分配内存(如`arr = new int[size]`)。 - 确认多维数组的维度定义(如`int arr[3][4]`)。 3. **error C2039(成员函数不存在)** - **原因**:调用了未在类中声明的函数,例如`MyView`类缺少某个成员函数。 - **解决**: - 在类的头文件(如`MyView.h`)中声明该函数。 - 检查基类(如`CView`)是否包含该虚函数,需通过`override`关键字确认。 4. **重复定义错误(LNK2005)** - **原因**:变量或函数在多个源文件中重复定义。 - **解决**: - 在头文件中使用`#pragma once`或`#ifndef/#define`防止重复包含。 - 将全局变量声明为`extern`(头文件中),并在一个源文件中定义。 --- ### 二、代码示例修正 以引用[2]的构造函数为例: ```cpp // MyView.cpp #include <OpenGl_GraphicDriver.hxx> // 添加缺失的头文件 void MyView::Initialize() { try { Handle(Aspect_DisplayConnection) aDisplayConnection; m_GraphicDriver = new OpenGl_GraphicDriver(aDisplayConnection); // 确保OpenGl_GraphicDriver已声明 } catch (Standard_Failure& e) { // 使用引用捕获异常 AfxMessageBox(L"Error occurred in initializing OpenCASCADE graphic driver."); } } ``` --- ### 三、项目配置检查 1. **包含目录**:确保OpenCASCADE等第三方库的头文件路径已添加到项目属性(`C/C++ > 附加包含目录`)。 2. **库目录**:添加库文件路径(`链接器 > 附加库目录`)并指定库名(如`OpenGl.lib`)。 3. **预处理器定义**:检查是否需要定义宏(如`WIN32`、`_DEBUG`)。 --- ### 四、常见问题排查流程 1. **从第一个错误开始修复**:后续错误可能是由首个错误引发。 2. **检查语法一致性**:如分号、括号是否缺失。 3. **清理并重建项目**:避免缓存导致的编译问题。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值