. Advertisement .
..3..
. Advertisement .
..4..
I have the following cpp code, but I do not know how to find the correct result. Why has this problem occurred, and how can it be solved?
Here is the code that I am running:
void main1()
{
const int MAX = 50;
class infix
{
private:
char target[MAX], stack[MAX];
char *s, *t;
int top, l;
public:
infix( );
void setexpr ( char *str );
void push ( char c );
char pop( );
void convert( );
int priority ( char c );
void show( );
};
void infix :: infix( ) //error
{
top = -1;
strcpy ( target, "" );
strcpy ( stack, "" );
l = 0;
}
void infix :: setexpr ( char *str )//error
{
s = str;
strrev ( s );
l = strlen ( s );
* ( target + l ) = '\0';
t = target + ( l - 1 );
}
void infix :: push ( char c )//error
{
if ( top == MAX - 1 )
cout << "\nStack is full\n";
else
{
top++ ;
stack[top] = c;
}
}
}
And this is the error text I receive:
"A function-declaration is not allowed here – before '{' token"
The cause: This error happens because of these following reasons:
}
in the main function.main
function.Solution: To solve this error, you need to classes’ function definitions and total classes outside of main (because it need to be in bound). Therefore, your program should be written as below:
Your
main
function has your classes’ function definitions in it, which is illegal. You can fix this by placing them outside. However, the entire class must be placed outside of main (since it is necessary to be within scope).While it is possible to include the entire class definition in your main, this is not the best way.