quantum231
quantum231

Reputation: 2593

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

Here is my program due to its minimum to reproduce the error:

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

Now the problem is that where I am using iwdith in the main function, it is underlined with red zig zag line. Hovering the mouse says "Error: expected a ']'"

Compiling produces these errors:

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

I am using Microsoft Visual Studio Community 2013. I do not understand why the IDE does not see iwidth when I have declared it!

Upvotes: 2

Views: 4780

Answers (2)

Baum mit Augen
Baum mit Augen

Reputation: 50061

As is, iwidth will be replaced with 5;. Get rid of the semicolon in the macro definition.

Also, you should not be using macros for this in the first place, this is not C. Furthermore, you seem to be abusing pointers. Get a better C++ book or tutorial.

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 190976

Don't end your #define with ;. That is copied down with the preprocessor.

Upvotes: 17

Related Questions