I have to write these two methods, to print out what is contained within:
ex is an array composed tokenType tk (sorry, trying to save space and avoid posting the whole structs)
Unfortunately, I am getting a compile error that says: error: no match for âoperator[]â in âex[i]â
How can I fix this so it will override <<, so that the second method uses the first?
ostream & operator<< ( ostream & os , const tokenType & tk)
{
switch (tk.category)
{
case TKN_OPRAND:
os << tk.operand;
break;
case TKN_OPRTOR:
os << tk.symbol;
break;
}
return os;
}
ostream & operator<< ( ostream & os , const expression & ex)
{
tokenType tk;
for (int i = 0; i < ex.numTokens; i++)
{
tk = ex[i];
os << tk.operand << " "; //problem line is this one
}
return os;
}
struct expression
{
int numTokens ;
tokenType tokens[MAX_TOKENS_IN_EXPRESSION] ;
void print() const ;
int toPostfix( expression & pfx ) const ;
int evalPostfix( int & val ) ;
expression() { numTokens = 0 ; } ; // default constructor
} ;
You are very close – you forgot to reference the
tokensarray, and tried indexing the expression itself. Naturally, the compiler complained, because you do not have a[]overloaded.This should work: