Error!! My project's due soon

I keep getting this error when I compile this code in Unix.
error: expected unqualified-id before "if"

The error is on the line of code starting the if statement:
#define grid segptr -> arrayint ManagerProcess::check_result(char t){
if ((grid[0][0] == t && grid[0][1] == t && grid[0][2] == t) ||(grid[1][0] == t && grid[1][1] == t && grid[1][2] == t) ||
(grid[2][0]== t && grid[2][1] == t && grid[2][2] == t) ||(grid[0][0] == t && grid[1][0] == t && grid[2][0] == t) ||
(grid[0][1] == t && grid[1][1] == t && grid[2][1] == t) || (grid[0][2] == t && grid[1][2] == t && grid[2][2] == t) ||
(grid[0][0] == t && grid[1][1] == t && grid[2][2] == t) ||(grid[0][2] == t && grid[1][1] == t && grid[2][0] == t) )
return 1;
else if ( grid[0][0] != ' ' && grid[0][1] != ' ' && grid[0][2] != ' ' && grid[1][0] != ' ' && grid[1][1] != ' ' &&
grid[1][2] != ' ' && grid[2][0] != ' ' && grid[2][1] != ' ' && grid[2][2] != ' ' )
return -1;
else return 0;
}

Note that this function is outside main()
Last edited on
In C/C++ certain things should technically be one continuous peice of text and not split across multiple lines with newlines.
For example: a string should be like this with no carriage returns or line breaks:
"I wandered lonely as a cloud, that floats on high o'er dales and hill. When all at once I saw a crowd, a host of golden daffodils. Beside the lake, beneath the trees, fluttering and dancing in the breeze."

As you can see this is very wide. In C/C++ to split text like this across multiple lines without confusing the compiler,
you do it like this using the line continuation indicator:

1
2
3
"I wandered lonely as a cloud, that floats on high o'er dales and hill. \
When all at once I saw a crowd, a host of  golden daffodils. \
Beside the lake, beneath the trees, fluttering and dancing in the breeze."


The \ character MUST BE THE LAST THING BEFORE the newline/carriage return.
This method also applies to #define macro substitution text. So your code would look line this:
1
2
3
4
5
6
7
8
9
10
11
#define grid segptr -> arrayint ManagerProcess::check_result(char t){ \
if ((grid[0][0] == t && grid[0][1] == t && grid[0][2] == t) ||(grid[1][0] == t && grid[1][1] == t && grid[1][2] == t) || \
(grid[2][0]== t && grid[2][1] == t && grid[2][2] == t) ||(grid[0][0] == t && grid[1][0] == t && grid[2][0] == t) || \
(grid[0][1] == t && grid[1][1] == t && grid[2][1] == t) || (grid[0][2] == t && grid[1][2] == t && grid[2][2] == t) || \
(grid[0][0] == t && grid[1][1] == t && grid[2][2] == t) ||(grid[0][2] == t && grid[1][1] == t && grid[2][0] == t) ) \
return 1; \
else if ( grid[0][0] != ' ' && grid[0][1] != ' ' && grid[0][2] != ' ' && grid[1][0] != ' ' && grid[1][1] != ' ' && \
grid[1][2] != ' ' && grid[2][0] != ' ' && grid[2][1] != ' ' && grid[2][2] != ' ' ) \
return -1; \
else return 0;\
} 




Last edited on
That macro will never work.
guestgulkan: I tried that but it still didn't work. Do you have any ideas? here's the exact code I used:

#define grid segptr -> arrayint ManagerProcess::check_result(char t){\
if ((grid[0][0] == t && grid[0][1] == t && grid[0][2] == t) ||(grid[1][0] == t && grid[1][1] == t && grid[1][2] == t) || \
(grid[2][0]== t && grid[2][1] == t && grid[2][2] == t) ||(grid[0][0] == t && grid[1][0] == t && grid[2][0] == t) || \
(grid[0][1] == t && grid[1][1] == t && grid[2][1] == t) || (grid[0][2] == t && grid[1][2] == t && grid[2][2] == t) || \
(grid[0][0] == t && grid[1][1] == t && grid[2][2] == t) ||(grid[0][2] == t && grid[1][1] == t && grid[2][0] == t) ) \
return 1;
else if ( grid[0][0] != ' ' && grid[0][1] != ' ' && grid[0][2] != ' ' && grid[1][0] != ' ' && grid[1][1] != ' ' && \
grid[1][2] != ' ' && grid[2][0] != ' ' && grid[2][1] != ' ' && grid[2][2] != ' ' )\
return -1;\
else return 0;\
}
You haven't done it right. You've missed a line.
This one:

return 1;
Last edited on
GUESTGULKAN: I changed it again to this:

#define grid segptr -> arrayint ManagerProcess::check_result(char t){\
if ((grid[0][0] == t && grid[0][1] == t && grid[0][2] == t) ||(grid[1][0] == t && grid[1][1] == t && grid[1][2] == t) ||\
(grid[2][0]== t && grid[2][1] == t && grid[2][2] == t) ||(grid[0][0] == t && grid[1][0] == t && grid[2][0] == t) ||\
(grid[0][1] == t && grid[1][1] == t && grid[2][1] == t) || (grid[0][2] == t && grid[1][2] == t && grid[2][2] == t) ||\
(grid[0][0] == t && grid[1][1] == t && grid[2][2] == t) ||(grid[0][2] == t && grid[1][1] == t && grid[2][0] == t) )\
return 1;\
else if ( grid[0][0] != ' ' && grid[0][1] != ' ' && grid[0][2] != ' ' && grid[1][0] != ' ' && grid[1][1] != ' ' &&\
grid[1][2] != ' ' && grid[2][0] != ' ' && grid[2][1] != ' ' && grid[2][2] != ' ' )\
return -1;\
else return 0;\
}


but now I get a different error in this:

Undefined first referenced
symbol in file
ManagerProcess::check_result(char) /var/tmp//cceOtX70.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

Note: This is C++ not C
Last edited on
Maybe - but this error is not associated with the previous problem.

What is saying now is that the linker cannot find the check_result(char t) function for the ManagerProcess class.

have you written it yet and/or inluded the header file??
That (check_result) is what I was trying to create in the source code
Maybe just change it to a normal function and have done with it. I don't think you can do what you want to do the way you are trying to do it.
See helios's comment earlier :-)
Last edited on
Why are you doing that as a macro?

1
2
3
4
5
6
7
for (int i = 0; i < 3; ++i)
 for (int j = 0; j < 3; ++j) {
  if (grid[i][j] != t)
   return 0;
  else if (grid[i][j] == '')
    return -1;  
 }


Doesn't do it exactly, but you can finish it up yourself.
Topic archived. No new replies allowed.