used undeclared identified

balancedparantheses
,
every StackNode
i got error saying used undeclared identified. how can i fix it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef DYNINTSTACK_H
#define DYNINTSTACK_H

class DynIntStack
{
private:
    // Structure for stack nodes
    struct StackNode
    {
        int value;        // Value in the node
        StackNode *next;  // Pointer to the next node
    };
    
    StackNode *top;      // Pointer to the stack top
    
public:
    // Constructor
    DynIntStack()
    {  top = NULL; }
    
    // Destructor
    ~DynIntStack();
    
    // Stack operations
    void push(int);
    void pop(int &);
    bool bracketsame(char,char);
    bool balancedparantheses(string); //checks if parentheses are
    bool isEmpty();
};
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "DynIntStack.h"
#include <iostream>
#include <string>
using namespace std;
    
// Main to run program
int main()
{
    string expression;
    
    cout<<"Enter an expression to check: ";
    cin>>expression;
    
[output]    if(balancedparantheses(expression))[/output] // I got error right here
        cout<<"Balanced expression \n";
    
    else
        cout<<"Expression is not balanced \n";
}
bool bracketsame(char opening,char closing) // function checks if opening and closing brackets are same
{
    if(opening == '(' && closing == ')') return true;
    else if(opening == '{' && closing == '}') return true;
    else if(opening == '[' && closing == ']') return true;
    return false;
}
bool balancedparantheses(string exp) //checks if parentheses are
{                                    //balanced or not
    stack<char> StackNode;
    for(int i =0;i<exp.length();i++)
    {
        if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
            StackNode.push(exp[i]);
        else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
        {
            if(StackNode.empty() || !bracketsame(StackNode.top(),exp[i]))
                return false;
            else
                StackNode.pop();
        }
    }
    return StackNode.empty() ? true:false;
}
You have two StackNodes. You made one a struct and made another a stack.
You need to declare the balancedparantheses (nice spelling there) function before main:

1
2
3
4
5
6
#include ...

bool balancedparantheses(string) ;

int main()
{ ... 


Note that the functions you have defined are not related to the member functions of the same name declared in the definition of DynIntStack, and that there is no stack type visible to the compiler in said function, although you try to use it on line 29.
Topic archived. No new replies allowed.