【问题标题】:Visual Studio, Error: expected a ']', when using a #define constant in array declarationVisual Studio,错误:在数组声明中使用 #define 常量时,应为“]”
【发布时间】:2016-01-19 00:30:35
【问题描述】:

这是我的程序,因为它重现错误的最低限度:

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

现在的问题是,我在 main 函数中使用 iwdith 时,它带有红色锯齿形线下划线。悬停鼠标说“错误:预期为']'”

编译会产生这些错误:

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

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

【问题讨论】:

    标签: c++ visual-c++ visual-studio-2013


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-02
        • 2020-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多