I am trying to call a function for my stack class. If I have all of the functions within the main file the project works, however, when called from the class it says the the Error: identifier "function name" is undefined. I think it is a simple syntax error, but i can't find it.
main.cpp
#include<iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "stack.h"
#define MAX 10
#define EMPTY -1
struct stack
{
char data[MAX];
int top;
};
int mystack::isempty(struct stack *s)
{
return (s->top == EMPTY) ? 1 : 0;
}
void mystack::emptystack(struct stack* s)
{
s->top=EMPTY;
}
void mystack::push(struct stack* s,int item)
{
if(s->top == (MAX-1))
{
printf("\nSTACK FULL");
}
else
{
++s->top;
s->data[s->top]=item;
}
}
char mystack::pop(struct stack* s)
{
char ret=(char)EMPTY;
if(!isempty(s))
{
ret= s->data[s->top];
--s->top;
}
return ret;
}
void mystack::display(struct stack s)
{
while(s.top != EMPTY)
{
printf("\n%d",s.data[s.top]);
s.top--;
}
}
int isoperator(char e)
{
if(e == '+' || e == '-' || e == '*' || e == '/' || e == '%' || e == '^')
return 1;
else
return 0;
}
int priority(char e)
{
int pri = 0;
if(e =='%' || e == '^')
pri = 3;
else
{
if (e == '*' || e == '/' || e =='%')
pri = 2;
else
{
if(e == '+' || e == '-')
pri = 1;
}
}
return pri;
}
void infix2postfix(char* infix, char * postfix, int insertspace)
{
char *i,*p;
struct stack X;
char n1;
emptystack(&X); // any time a class like this is called it says Error: identifier "emptystack"
// is undefined
i = &infix[0];
p = &postfix[0];
while(*i)
{
while(*i == ' ' || *i == '\t')
{
i++;
}
if( isdigit(*i) || isalpha(*i) )
{
while( isdigit(*i) || isalpha(*i))
{
*p = *i;
p++;
i++;
}
if(insertspace)
{
*p = ' ';
p++;
}
}
if( *i == '(' )
{
push(&X,*i);
i++;
}
if( *i == ')')
{
n1 = pop(&X);
while( n1 != '(' )
{
*p = n1;
p++;
if(insertspace)
{
*p = ' ';
p++;
}
n1 = pop(&X);
}
i++;
}
if( isoperator(*i) )
{
if(mystack::isempty(&X))
push(&X,*i);
else
{
n1 = pop(&X);
while(priority(n1) >= priority(*i))
{
*p = n1;
p++;
if(insertspace)
{
*p = ' ';
p++;
}
n1 = pop(&X);
}
push(&X,n1);
push(&X,*i);
}
i++;
}
}
while(!isempty(&X))
{
n1 = pop(&X);
*p = n1;
p++;
if(insertspace)
{
*p = ' ';
p++;
}
}
*p = '\0';
}
int main()
{
char in[50],post[50];
strcpy(&post[0],"");
printf("Enter Infix Expression : ");
fflush(stdin);
gets(in);
infix2postfix(&in[0],&post[0],1);
printf("Postfix Expression is : %s\n",&post[0]);
return 0;
}
stack.h
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
using namespace std;
class mystack
{
public:
int isempty(struct stack *s);
void emptystack(struct stack* s);
void push(struct stack* s,int item);
char pop(struct stack* s);
void display(struct stack s);
};
I am using visual studio if that helps.
EDIT: added comment for clarity.
Thanks,
Ryan
At a cursory glance, this function:
void emptystack(struct stack* s)
{
s->top=EMPTY;
}
Is missing the scope operator (::), so you probably intended to write it as:
void mystack::emptystack(struct stack* s)
{
s->top=EMPTY;
}
I'm not sure if that's your problem though, since "I'm trying to call a function" is a bit vague. You might want to narrow down precisely where the error is occurring, then edit your question with additional information.
Edit: In looking at your implementation a bit more, I'm not sure why you created the mystack class at all. It looks like you just want to define a bunch of functions that operate on your stack struct, which doesn't require a class definition. If you want to do it this way for some unusual reason, then you'll have to instantiate a mystack object before you can call its member functions. Something of the nature:
mystack * myStackObj = new mystack();
myStackObj->emptystack(&X);
Though I'm not sure why you would want to do this. The other alternative is to roll your stack struct into the class instead, either by making the whole struct a member of the class or by simply adding data and top to the class. Then if you instantiated a mystack object it would have the data of the stack and could call methods on its own data. I'd also suggest looking at a tutorial/documentation/book related to C++ classes and their usage. Here's one, but there are undoubtedly plenty of others.
Related
I have to write a program which balances a string with brackets.I wrote the program but it doesn't matter which string I enter because the program always says that the string is good.
Here's the code:
header file
#ifndef HEADER_H_
#define HEADER_H_
#include <string>
struct Element {
char data;
Element* link;
};
typedef Element* Stack;
void initStack(Stack& S);
void push(Stack& S, int a);
void pop(Stack &S);
int top(Stack& S);
bool isEmpty(Stack &S);
bool goodPair(char deschis, char inchis);
bool check(std::string s);
#endif
functions file
#include <iostream>
#include <string>
#include "header.h"
using namespace std;
void initStack(Stack& S)
{
S = nullptr;
}
void push(Stack& S, int a)
{
Element*nou = new Element;
nou->data = a;
nou->link = S;
S = nou;
}
void pop(Stack& S)
{
Stack aux = S;
S = S->link;
delete(aux);
}
int top(Stack& S)
{
if (isEmpty(S))
return int();
return S->data;
}
bool isEmpty(Stack &S)
{
if (S == 0)
return true;
else
return false;
}
bool goodPair(char deschis, char inchis)
{
if (deschis == '(' && inchis == ')')
return true;
else if (deschis == '[' && inchis == ']')
return true;
else if (deschis == '{' && inchis == '}')
return true;
else if (deschis == '<' && inchis == '>')
return true;
else
return false;
}
bool check(std::string s)
{
Element* S;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == '(' || s[i] == '[' || s[i] == '{' || s[i] == '<')
push(S, s[i]);
else
{
if (s[i] == ')' || s[i] == ']' || s[i] == '}' || s[i] == '>')
if (isEmpty(S) || !goodPair(top(S), s[i]))
return false;
else
pop(S);
}
}
if (isEmpty(S))
return false;
else
return true;
}
main file
#include <iostream>
#include <string>
#include "header.h"
using namespace std;
int main()
{
Stack S;
initStack(S);
string s;
cout << "Write the string:";
cin >> s;
if (check(s))
cout << "Good";
else
cout << "Bad";
return 0;
}
I used a stack and I traversed each character.If the character is an opening bracket I put it in the stack.When the character is a closing bracket, I compare it with the top of the stack.If it's good I pop the top of the stack.
You create a pointer to Element (aliased as Stack) S in main() and initialize it with nullptr using initStack() and then you do not use this variable anymore. Instead you create a local S in function check() and use it uninialized, which leads to UB.
Looks like you get confused by naming as you call of them S (variable in main(), variable in check(), all reference parameters all called S). It is not illegal to do so, but looks like you confused yourself. (you even called std::string lowercase s to increase confusion)
Also you have logical error in your function:
if (isEmpty(S))
return false;
else
return true;
should be opposite, if stack is empty then string is balanced, not vice versa. So replace it with:
return isEmpty( S );
Hello Im new to programming and I'm trying to make a RPN calculator in Cpp. I sort of understand how it works but i dont know how to code it. So after searching on youtube i found a video of someone with a Stack calulator im planning to use on my project. I copied everything the program runs but it doesnt display the desired result. I debugged on Visual Studio and it gives me the error {data=0xcccccccccccccccc top=-858993460 size=-858993460 }. The program keeps running but doesnt do what it is supposed to do. any help would be appreciate it.
This is the firs file that defines the stack
#pragma once
// arraystack.h
#define STACK_H
template <class T>
class stack
{
private:
T* data; // Pointer.
int top;
int size;
void resize();
bool needToResize();
public:
stack()
{
size = 5;
data = new T[size];
top = 0;
}
void push(T item);
T peek();
T pop();
bool isEmpty();
};
//#include "arraystack.cpp"
#include <stdexcept> // para manejar la excepcion de fuera de rango.
template <class T>
void stack<T>::push(T item)
{
if (needToResize())
resize();
data[top] = item;
top++;
}
template < class T >
T stack<T>::peek()
{
if (top <= 0)
throw std::out_of_range("Attempting to peek an empty stack.");
return data[top - 1];
}
template<class T>
T stack<T>::pop()
{
if (top <= 0)
throw std::out_of_range("Attempting to pop an empty stack.");
top--;
return data[top];
}
template <class T>
bool stack<T>::needToResize()
{
return (top == size);// si top es igual al size entonce sse necesita resize.
}
template <class T>
void stack<T>::resize()
{
T* newdata = new T[2 * size];
for (int i = 0; i < size; i++)
newdata[i] = data[i];
data = newdata;
size *= 2;
}
template <class T>
bool stack<T>::isEmpty()
{
return (top == 0);
}
This is the second file that defines the calculator
#pragma once
// calculator.h
#include <iostream>
#include <string>
#include "arraystack.h"
using namespace std;
// check if char is a digit.
bool isDigit(char c)
{
return (c <= '0' && c <= '9'); // valor unicode de los numeros en 48 empieza 0.
}
//check if char is an operator
bool isOp(char c)
{
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')' || c == '^');
}
// determine the precedence of an operator
int getPrecendence(char c)
{
switch (c)
{
case '+':
case '-': return 1;
case '*':
case '/': return 2;
case '(':
case ')': return 3;
default: return -1;
}
}
// evaluate an arithmetic expression.
int operate(int val1, int val2, char op)
{
if (op == '+') return val1 + val2;
if (op == '-') return val1 - val2;
if (op == '*') return val1 * val2;
return val1 / val2; // se retorna esta porque es la unica op left. (sin expo)
}
//evaluate a string.
int evaluate(string s)
{
stack<int> vals;
stack<char> ops;
int val = 0;
int pos = 0;
while (pos < s.length())
{
char spot = s[pos];
if (isDigit(spot))
{
val = (val * 10) + (int)(spot - '0');
}
else if (isOp(spot))
{
if (spot == '(')
{
ops.push(spot);
val = 0;
}
else if (vals.isEmpty())
{
vals.push(val);
ops.push(spot);
val = 0;
}
else if (spot == ')')
{
vals.push(val);
while (ops.peek() != '(')
{
spot = ops.pop();
val = vals.pop();
int prev = vals.pop(); // previous on the stack
val = operate(prev, val, spot);
vals.push(val);
}
ops.pop();
vals.pop();
}
else
{
char prev = ops.peek();
if (getPrecendence(spot) > getPrecendence(prev))
{
vals.push(val);
ops.push(spot);
val = 0;
}
else
{
int prevval = vals.pop();
int prevop = ops.pop();
prevval = operate(prevval, val, prevop);
vals.push(prevval);
ops.push(spot);
val = 0;
}
}
}
pos++;
}
while (!ops.isEmpty())
{
int prev = vals.pop();
char spot = ops.pop();
val = operate(prev, val, spot);
}
return val;
}
This is the file that tests
// arraystack.cpp (the tester class)
#include <iostream>
#include "calculator.h"
#include <string>
using namespace std;
int main()
{
cout << evaluate("3 + 4 + 5") << endl; // expected result 12
cout << evaluate("3 * 4 + 5") << endl; // expected result 17
cout << evaluate("3 + 4 * 5") << endl; // expected result 23
cout << evaluate("(3 +4) * 5") << endl; // expected result 35
system("pause");
return 0;
}
The results i get
Results after running program
The errors showing in the visual studio debugger
Visual Studio Debbuging screen
I have no idea of whats going on. Any help would be appreciate it.
The output i get:
-1667666
-1667666
-1667666
-9658676
Press any key to continue . . .
I need to check if number of letters "a" is equal to number of letters "b" using stack.
So i understand logic of this task, but my code doesn't work.
Logic:
If current letter == to letter in stack (s.pop()) or stack is empty then push into stack
else pop from stack
after end of cycle check size of stack. If it is empty so number of letters is equl, else not
I already have class stack
#include <string>
#include <iostream>
#include <cstdlib> // для system
using namespace std;
class stack {
public:
stack() {
ptr = 0;
}
~stack() {}
bool push(int val) {
if (ptr >= MAXSIZE) return false;
body[ptr++] = val; return true;
}
bool pop(int *val) {
if (ptr == 0) return false;
*val = body[--ptr]; return true;
}
bool empty() {
return ptr == 0;
}
private:
enum { MAXSIZE = 100 };
int body[MAXSIZE];
int ptr; // указатель на последний элемент
};
int main()
{
stack s;
std::string str;
std::cout << "Enter your ab string ";
getline(std::cin, str);
for (int c : str) {
if (c == s.pop(&c) || s.empty()) {
s.push(c);
}
else {
s.pop(&c);
}
}
if (s.empty()) {
cout << "YES\n";
system("pause");
return 0;
}
else {
cout << "NO\n";
system("pause");
}
}
result for abab, aabb, ab 'YES'
for aaabb, aba 'NO'
You need a method to look at current value on top of stack without popping it:
class stack {
...
int top() { // never use on an empty stack
return body[ptr-1];
}
...
};
That way you can write:
for (int c : str) {
// short circuit evaluation ensures that top is never called on an empty stack
if (s.empty() || (c == s.top()) {
s.push(c);
}
else {
s.pop(&c);
}
If you cannot, you must push back the popped value if it should not have been popped:
for (int c : str) {
int d;
if (! s.pop(&d)) { // s was empty
s.push(c);
}
else if (c == d) {
s.push(d); // should not have been popped
s.push(c);
}
}
You can push everytime you see a.
for (int c = 0; c < str.size() ; ++c) {
if (str[c] == 'a') s.push('a');
}
if ((s.size() * 2) == str.size()) cout << "YES\n"; else cout << "NO\n";
stack::size can be implemented this way:
int stack::size() {
return ptr;
}
I'm writing a program that uses stacks to evaluate infix expressions read in from a file. Here is the code:
ptStack.h
#ifndef STACK
#define STACK
#include <cstdlib>
#include <iostream>
using namespace std;
template <class Item>
class Stack
{
public:
// CONSTRUCTOR
Stack( ) {top = NULL; count = 0;} // Inline
// MODIFIER MEMBER FUNCTIONS
void push( const Item& entry);
Item pop( );
// CONSTANT MEMBER FUNCTIONS
int size( ) {return count;} // Inline
bool is_empty( ) {return count == 0;} // Inline
private:
// DATA MEMBERS
struct Node
{
Item element;
Node *next;
};
Node *top;
int count;
};
#endif
ptStack.cpp
#include <cassert>
#include "ptStack.h"
using namespace std;
// MODIFIER MEMBER FUNCTIONS
template <class Item>
void Stack<Item>::push(const Item& entry)
{
Node *temp;
temp = new Node;
temp->element = entry;
temp->next = top->next;
top->next = temp;
count++;
}
template <class Item>
Item Stack<Item>::pop( )
{
Item value;
Node *temp;
value = top->next->element;
temp = top->next;
top->next = top->next->next;
delete temp;
count--;
return value;
}
infix.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "ptStack.h"
using namespace std;
// PRECONDITION: op must be an operator
// POSTCONDITION: returns precedence of operator
int pr(char op)
{
int precedence = 0;
switch (op)
{
case '+':
case '-': precedence = 1;
case '*':
case '/': precedence = 2;
default : precedence = 0;
}
return precedence;
}
// PRECONDITIONS: optr is one of the following: + - * /
// opnd1 and opnd2 are numbers from 1-9
// POSTCONDITIONS: returns the result of the chosen mathematical operation
int apply(char optr, int opnd1, int opnd2)
{
int result;
switch (optr)
{
case '+': result = opnd2+opnd1;
case '-': result = opnd2-opnd1;
case '*': result = opnd2*opnd1;
default : result = opnd2/opnd1;
}
return result;
}
int main()
{
Stack<int> numbers;
Stack<char> operators;
char ch, optr;
int num, opnd1, opnd2, prec = 0, newprec;
ifstream in_file; // input file
char in_file_name[20]; // name of input file (20 letter max)
cout << "Enter input file name: ";
cin >> in_file_name;
in_file.open(in_file_name); // opens file to read equations
while (!in_file.eof())
{
cout << "Expression: ";
while(in_file >> ch)
{
if (ch == ' ')
{}
else
{
num = ch - '0';
if((num < 10) && (num > 0))
{
cout << num << " ";
numbers.push(num);
}
else if((ch == '+') || (ch == '-') || (ch == '*') || (ch == '/'))
{
cout << ch << " ";
newprec = pr(ch);
if(newprec >= prec)
{
prec = newprec;
operators.push(ch);
}
else if(newprec < prec)
{
optr = operators.pop( );
opnd1 = numbers.pop( );
opnd2 = numbers.pop( );
num = apply(optr, opnd1, opnd2);
numbers.push(num);
operators.push(ch);
}
}
}
if(in_file.peek() == '\n')
break;
}
num = operators.size();
while(num != 0)
{
optr = operators.pop( );
opnd1 = numbers.pop( );
opnd2 = numbers.pop( );
num = apply(optr, opnd1, opnd2);
numbers.push(num);
num = operators.size( );
}
num = numbers.pop( );
cout << endl << "Value = " << num << endl << endl;
}
return 0;
}
It looks like everything should work but when I compile it, I get this error message.
> g++ -c ptStack.cpp
> g++ infix.cpp ptStack.o
Undefined first referenced
symbol in file
_ZN5StackIcE4pushERKc /var/tmp//ccwhfRAZ.o
_ZN5StackIiE4pushERKi /var/tmp//ccwhfRAZ.o
_ZN5StackIcE3popEv /var/tmp//ccwhfRAZ.o
_ZN5StackIiE3popEv /var/tmp//ccwhfRAZ.o
ld: fatal: symbol referencing errors. No output written to a.out
I've been able to pinpoint the errors to the callings of the push and pop member functions in the main of the infix file. I tried defining them inline in the header file like the size function and it compiles just fine using g++ infix.cpp ptStack.h, but then I get a segmentation fault when I run it.
Does anyone know how to fix either of these errors? Thanks in advance.
Just compile all the .cpp files with g++
g++ ptStack.cpp infix.cpp
I wrote this program and it supposed to test for the correct use of the three grouping symbols "(",")";"[","]"; and "{","}". It is using the array implementation of the stacks and supposed to evaluate if it is good string or a bad string. For example: (a+b), [(a-b)+c] would be good and )a+b( etc. would be bad string. When i run the program i get only one error. I thought i am missing a semi-colon or something, but after looking through the code several time,i can't find it. Maybe i got tunnel vision. Can you please see what the problem here is? This is the error: project1.cpp:41: error: expected initializer before 'while'.
#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;
const int DefaultListSize = 100;
typedef char Elem;
class Astack {
private:
int size;
int top;
Elem *listArray;
public:
Astack (int sz = DefaultListSize)
{size = sz; top= 0; listArray = new Elem[sz];}
~Astack() {delete [] listArray;}
void clear() {top=0;}
bool push(const Elem& item) {
if (top == size) return false;
else {listArray[top++] = item; return true;}}
bool pop(Elem& it) {
if (top==0) return false;
else {it = listArray[--top]; return true;}}
bool topValue(Elem& it) const {
if (top==0) return false;
else {it = listArray[top-1]; return true;}}
bool isEmpty() const {if (top==0) return true;
else return false;}
int length() const{return top;}
}; //end of class Astack
Astack s;
const string LEFTGROUP="([{";
const string RIGHTGROUP=")]}";
int main()
while (!EOF) {
while (!EOL) {
ch = getc();
if (ch == LEFTGROUP[0]) {
s.push(ch);
}
if (ch == LEFTGROUP[1] {
s.push(ch);
}
if (ch == LEFTGROUP[2] {
s.push(ch);
}
} //checking for openers
while (!EOL) {
ch = getc();
if (s.top() == LEFTGROUP[0]) {
if (ch == RIGHTGROUP[0]) {
s.pop();
}
}
if (s.top() == LEFTGROUP[1]) {
if (ch == RIGHTGROUP[1]) {
s.pop();
}
}
if (s.top() == LEFTGROUP[2]) {
if (ch == RIGHTGROUP[2]) {
s.pop();
}
}
if (!s.empty()) {
cout<<"Bad String."<<endl;
else {
cout<<"Good String."endl;
}
}
}
return 0;
You forgot a { at the beginning of int main(). You should also end with }
int main(){
//your while code
return 0;
}