heron
heron

Reputation: 3661

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

enter image description here

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

Upvotes: 0

Views: 596

Answers (4)

trumpetlicks
trumpetlicks

Reputation: 7065

#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.

Upvotes: 2

tskuzzy
tskuzzy

Reputation: 36446

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.

Upvotes: 1

Chad
Chad

Reputation: 19032

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.

Upvotes: 4

Xeo
Xeo

Reputation: 131789

#defines 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.

Upvotes: 8

Related Questions