I have come across this error: ‘Symbol ‘pa’ could not be resolved’ in one of my methods.
The code:
PredefinedAtom* PredefinedSymbolTableCollection::findAtomByLexCode(u_int lex_code) {
// PredefinedAtom* pa; // <== ACCEPTED
for(u_int i = 0; i < pst.size(); i++) {
if((PredefinedAtom* pa = pst[i]->findAtomByLexCode(i)) != NULL){// <== BAD
// some code
}
}
}
If I declare a type of PredefinedAtom outside of the ‘if’ scope the code compiles/runs. However if I try declaring a type inside of ‘if’ the compiler issues a ‘Cannot resolve ‘pa’ symbol’ error. ‘findAtomByLexCode’ either returns a pointer to ‘PredefinedAtom*’ or NULL.
You are allowed to define a variable in the condition of an
ifstatement but you can’t define it in an expression. You need to write it like this:The value of
pais converted toboolto determine which branch is taken.