Visual Studio,错误:在数组声明中使用 #define 常量时,应使用"]"

Visual Studio, Error: expected a ']', when using a #define constant in array declaration

本文关键字:#define 常量 错误 Studio 声明 数组 Visual      更新时间:2023-10-16
这是我的程序,因为它重现

错误的最小值:

#include <iostream>
#include <Windows.h>
#include <fstream>
using namespace std;
#define iwidth 5;
#define iheight 3;
int main()
{
    struct rgb_data_struct {
        BYTE B;
        BYTE G;
        BYTE R;
    };
    rgb_data_struct **image;
    image = new rgb_data_struct *[iwidth];
    for (int i = 0; i < iwdith; i++)
    {
    }
    return 0;
}

现在的问题是,当我在主函数中使用 iwdith 时,它用红色之字形线下划线。将鼠标悬停显示"错误:预期为']'"

编译会产生以下错误:

Error   1   error C2143: syntax error : missing ')' before ';'  c:usersuser0documentscppbitmapbitmapmain.cpp 51  1   bitmap
Error   2   error C2143: syntax error : missing ']' before ')'  c:usersuser0documentscppbitmapbitmapmain.cpp 51  1   bitmap
Error   3   error C2143: syntax error : missing ';' before ')'  c:usersuser0documentscppbitmapbitmapmain.cpp 51  1   bitmap
Error   4   error C2143: syntax error : missing ';' before ']'  c:usersuser0documentscppbitmapbitmapmain.cpp 51  1   bitmap
Error   5   error C2065: 'iwdith' : undeclared identifier   c:usersuser0documentscppbitmapbitmapmain.cpp 52  1   bitmap
    6   IntelliSense: expected a ']'    c:Usersuser0DocumentsCppbitmapbitmapmain.cpp 51  32  bitmap
    7   IntelliSense: expected an expression    c:Usersuser0DocumentsCppbitmapbitmapmain.cpp 51  38  bitmap
    8   IntelliSense: identifier "iwdith" is undefined  c:Usersuser0DocumentsCppbitmapbitmapmain.cpp 52  22  bitmap

我正在使用Microsoft Visual Studio Community 2013。我不明白为什么 IDE 在我声明它时看不到 iwidth!

不要用;结束你的#define。这是用预处理器复制下来的。

按原样,iwidth将被替换为 5; 。去掉宏定义中的分号。

另外,您首先不应该为此使用宏,这不是 C。此外,您似乎在滥用指针。获取更好的C++书籍或教程。