problem in separate files..

hi, i have a problem in separate this code into two file thats in infix.h and infix.cpp
plz help me..

#include <iostream>
#include <string>
#include <stack>
using namespace std;

void Convert(const string & Infix, string & Postfix);
bool IsOperand(char ch);
bool TakesPrecedence(char OperatorA, char OperatorB);

int main(void)
{
char Reply;
do
{
string Infix, Postfix; // local to this loop
cout << "Enter an infix expression (e.g. (a+b)/c^2, with no spaces):"<< endl;
cin >> Infix;
Convert(Infix, Postfix);
cout << "The equivalent postfix expression is:" << endl<< Postfix << endl;
cout << endl << "Do another (y/n)? ";
cin >> Reply;
}
while (tolower(Reply) == 'y');
return 0;
}

/*
Given: ch A character.
Task: To determine whether ch represents an operand (here
understood to be a single letter or digit).
Return: In the function name: true, if ch is an operand, false
otherwise.

*/

bool IsOperand(char ch)
{
if (((ch >= 'a') && (ch <= 'z')) ||
((ch >= 'A') && (ch <= 'Z')) ||
((ch >= '0') && (ch <= '9')))
return true;
else
return false;
}


bool TakesPrecedence(char OperatorA, char OperatorB)
{
if (OperatorA == '(')
return false;
else if (OperatorB == '(')
return false;
else if (OperatorB == ')')
return true;
else if ((OperatorA == '^') && (OperatorB == '^'))
return false;
else if (OperatorA == '^')
return true;
else if (OperatorB == '^')
return false;
else if ((OperatorA == '*') || (OperatorA == '/'))
return true;
else if ((OperatorB == '*') || (OperatorB == '/'))
return false;
else
return true;

}



void Convert(const string & Infix, string & Postfix)
{
stack<char> OperatorStack;
char TopSymbol, Symbol;
int k;
for (k = 0; k < Infix.size(); k++)
{
Symbol = Infix[k];
if (IsOperand(Symbol))
Postfix = Postfix + Symbol;
else
{
while ((! OperatorStack.empty()) &&
(TakesPrecedence(OperatorStack.top(), Symbol)))
{
TopSymbol = OperatorStack.top();
OperatorStack.pop();
Postfix = Postfix + TopSymbol;
}
if ((! OperatorStack.empty()) && (Symbol == ')'))
OperatorStack.pop(); // discard matching (
else
OperatorStack.push(Symbol);
}
}
while (! OperatorStack.empty())
{
TopSymbol = OperatorStack.top();
OperatorStack.pop();
Postfix = Postfix + TopSymbol;
}
}

thank you in advance
help me please
Move the function definitions into the header file, and then include the header file at the top of your cpp file.
hi, thanks.. i did the separation as said bt still got 1 error i dont know how to over come it here is the code i did

infix1.h


#include <iostream>
#include <string>
#include <stack>
using namespace std;

class infix1
{
public:
void Convert(const string & Infix, string & Postfix);
bool IsOperand(char ch);
bool TakesPrecedence(char OperatorA, char OperatorB);
};


infix.cpp

#include "infix1.h"
#include <iostream>
#include <string>
#include <stack>

int main(void)
{
char Reply;
do
{
string Infix, Postfix; // local to this loop
cout << "Enter an infix expression (e.g. (a+b)/c^2, with no spaces):"<< endl;
cin >> Infix;
Convert(Infix,Postfix);// here is the error said that identifier not found
cout << "The equivalent postfix expression is:" << endl<< Postfix << endl;
cout << endl << "Do another (y/n)? ";
cin >> Reply;
}
while (tolower(Reply) == 'y');
return 0;
}

/*
Given: ch A character.
Task: To determine whether ch represents an operand (here
understood to be a single letter or digit).
Return: In the function name: true, if ch is an operand, false
otherwise.

*/

bool infix1::IsOperand(char ch)
{
if (((ch >= 'a') && (ch <= 'z')) ||
((ch >= 'A') && (ch <= 'Z')) ||
((ch >= '0') && (ch <= '9')))
return true;
else
return false;
}

/* Given:
OperatorA A character representing an operator or
parenthesis.
OperatorB A character representing an operator or
parenthesis.
Task: To determine whether OperatorA takes precedence over OperatorB.

Return: In the function name: true, if OperatorA takes precedence
over OperatorB.
*/
bool infix1::TakesPrecedence(char OperatorA,char OperatorB)
{
if (OperatorA == '(')
return false;
else if (OperatorB == '(')
return false;
else if (OperatorB == ')')
return true;
else if ((OperatorA == '^') && (OperatorB == '^'))
return false;
else if (OperatorA == '^')
return true;
else if (OperatorB == '^')
return false;
else if ((OperatorA == '*') || (OperatorA == '/'))
return true;
else if ((OperatorB == '*') || (OperatorB == '/'))
return false;
else
return true;

}

/* Given: Infix A string representing an infix expression (no spaces).
Task: To find the postfix equivalent of this expression.
Return: Postfix A string holding this postfix equivalent.
*/

void infix1::Convert(const string & Infix,string & Postfix)
{
stack<char> OperatorStack;
char TopSymbol, Symbol;
int k;
for (k = 0; k < Infix.size(); k++)
{
Symbol = Infix[k];
if (IsOperand(Symbol))
Postfix = Postfix + Symbol;
else
{
while ((! OperatorStack.empty()) &&
(TakesPrecedence(OperatorStack.top(), Symbol)))
{
TopSymbol = OperatorStack.top();
OperatorStack.pop();
Postfix = Postfix + TopSymbol;
}
if ((! OperatorStack.empty()) && (Symbol == ')'))
OperatorStack.pop(); // discard matching (
else
OperatorStack.push(Symbol);
}
}
while (! OperatorStack.empty())
{
TopSymbol = OperatorStack.top();
OperatorStack.pop();
Postfix = Postfix + TopSymbol;
}
}
This error occurs because you are trying to use a function before you define it. You must move the definition to somewhere that is before where you try to use it.


That means everything after

void infix1::Convert(const string & Infix,string & Postfix)

must be moved. I suggest you move it into the h file.

Alternatively, move the main function to the end of your cpp file, so that it is after the definition of Convert.
Last edited on
Topic archived. No new replies allowed.