简体   繁体   中英

Strange C++ console application error

Code looks like that

#include <cstdlib>
#include <iostream>

using namespace std;
#define n 3;
#define m 4;

int main(int argc, char* argv[])
{
    int arr[n][m];
    bool f=true;
    for (int i=0; i<n; i++)
        for (int j=0; j<m; j++)
            cin>>arr[i][j];
    for (int i=0; i<n; i++)
        for (int j=0; j<m; j++)
            if(arr[i][j]!=a[0][j])
                f=false;
    if(f)
            cout<<"Setirler eynidir.";
    else 
        cout<<"muxtelifdir";
    system("pause");
    return 0;
}

Getting bunch of errors

在此处输入图片说明

I can't see any problematic piece of code. Any suggestions? What am I missing?

#define s are not terminated with ; , so the semicolon is actually pasted there too, yielding

int arr[3;][4;];

which is invalid code.

Always remember, avoid the preprocessor like the plague.

Since #define is a preprocessor directive, you're making some mistakes in your array delcaration.

#define n 3;
#define m 4;

int arr[m][n];

// This translates into 
int arr[3;][4;];

You could fix it by removing the ; after the defines:

#define n 3
#define m 4

Or even better:

static const size_t n = 3;
static const size_t m = 3;

As the above will give you type safety elsewhere that you may use n or m .

#define 

statements don't have semi-colons after them.

[EDIT1] Actually they can but, be careful as preprocessor statements like this become strict text replacement.

Remove the semi-colon in your #define .

define's are expanded by more or less copy+pasting the contents into your source file. Thus

int arr[n][m];

is expanded into

int arr[3;][4;];

which is clearly a syntax error. Same goes with your for-loops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM