Shehlizah Malik Shehlizah Malik - 1 month ago 9
C++ Question

Code is not working. Using Stacks

Here's my code for checking balanced parenthesis which takes an expression and checks if user entered the expression correctly but it isn't working. It gives an error. I don't think so there is an error in the public thing.
Please Help!

class dynamicStack {
struct node{
char num;
node *next;
};
public:
node *top;
dynamicStack(){
top=NULL;
}
void push(char);
void pop();
void check(string);
};
void check(string exp) {
dynamicStack dS;
char c[100];
for(int i=0;i<sizeof(exp);i++) {
c[i]=exp[i];
}

int i=0;
while(c[i]!='\0') {
if(c[i]=='(' || c[i]=='[') {
dS.push(c[i]);
}
if(c[i]==')' || c[i]==']'){
if ((dS.top)==0)
cout<<"Not Balanced\n";
else
dS.pop();
}
}
}

void dynamicStack::pop(){
node *temp;
temp=top;
if(top == NULL) {
cout<<"Stack is empty"<<endl;
}
else
cout<<"Deleting number: "<<temp->num<<endl;
top=top->next;
delete temp;
}
void dynamicStack::push(char c) {
node *newNode;
newNode = new node;
newNode->num=c;
newNode->next=top;
top=newNode;
}
int _tmain(int argc, _TCHAR* argv[]) {
dynamicStack dS;
string exp;
cout<<"Enter an expression: ";
cin>>exp;
dS.check(exp);

system("pause");

return 0;
}


It gives the following error:

1>ds-2.obj : error LNK2019: unresolved external symbol "public: void _thiscall dynamicStack::check(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (? check@dynamicStack@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _wmain

Answer

Your implementation of

void check(string exp)

does not mention its class scope. It must read:

void dynamicStack::check(string exp) {
...
}

BTW this is exactly what the linker message is trying to tell you. When you are getting these kind of errors you often simply got something like the above wrong.