LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c++ error (https://www.linuxquestions.org/questions/programming-9/c-error-367478/)

sajith 09-27-2005 08:20 AM

c++ error
 
hai sir

i have got a error while programming in C++

i do program for conversion of infix expression to postfix form

in the convert() function

i got a error

void convert(const string & Infix,string & Postfix)
{
Stack<char> opstack;
char Topsymbol,symbol;
int k;

for(k=0;k<sizeof(Infix);++k)
{
symbol=Infix[k];
if(IsOperand(symbol))
// cout<<Postfix<<symbol;
//Postfix=Postfix+symbol;
strcat(Postfix,symbol);

else
{
while((!opstack.IsEmpty())&&(precedence(opstack.IsFull(),symbol)))
{
Topsymbol=opstack.IsFull();
opstack.pop();
// cout<<Postfix<<Topsymbol;
//Postfix=Postfix+Topsymbol;
strcat(Postfix,Topsymbol);
}

if((!opstack.IsEmpty())&&(symbol==')'))
opstack.pop();
else
opstack.push(symbol);
}
}

while(!opstack.IsEmpty())
{
Topsymbol=opstack.IsFull();
opstack.pop();
//cout<<Postfix<<Topsymbol;
//Postfix=Postfix+Topsymbol;
strcat(Postfix,Topsymbol);
}

}

/**

void operator+(string & Postfix,char ch)
{
strcpy(Postfix,ch);

cout<<Postfix<<"\t"<<ch;
strcat(Postfix,ch);
return Postfix;
}
**/

the error is

: In function ‘void convert(const std::string&, std::string&)’:
error: cannot convert ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >’ to ‘char*’ for argument ‘1’ to ‘char* strcat(char*, const char*)’


please help me to solve this error

i humbly request to tell the solution

jtshaw 09-27-2005 08:40 AM

Code:

void convert(const string & Infix,string & Postfix)
{
    Stack<char> opstack;
    char Topsymbol,symbol;
    int k;

    for(k=0;k<sizeof(Infix);++k)
    {
        symbol=Infix[k];
        if(IsOperand(symbol))
            //cout<<Postfix<<symbol;
        //Postfix=Postfix+symbol;
        strcat(Postfix,symbol);

        else
        {
            while((!opstack.IsEmpty())&&(precedence(opstack.IsFull(),symbol)))
            {
                Topsymbol=opstack.IsFull();
                opstack.pop();
                // cout<<Postfix<<Topsymbol;
                //Postfix=Postfix+Topsymbol;
                strcat(Postfix,Topsymbol);
            }

            if((!opstack.IsEmpty())&&(symbol==')'))
                opstack.pop();
            else
                opstack.push(symbol);
        }
    }

    while(!opstack.IsEmpty())
    {
        Topsymbol=opstack.IsFull();
        opstack.pop();
        //cout<<Postfix<<Topsymbol;
        //Postfix=Postfix+Topsymbol;
        strcat(Postfix,Topsymbol);
    }

}

Please use code blocks, it is much easier for us to read.

Anyway... your problem is strcat doesn't take in a string object, it takes in a "c string" or char *. I'd think the + operator would be good enough for you since that is overloaded to do concatenation on the stl string class.

I also recommend you start using { and } on if statements at all times. Especially in this case here:

Code:

if(IsOperand(symbol))
            //cout<<Postfix<<symbol;
        //Postfix=Postfix+symbol;
        strcat(Postfix,symbol);

If you uncomment out the cout or Postfix= line then it dramatically changes what this code is likely to do.

Code:

if(IsOperand(symbol)) {
            //cout<<Postfix<<symbol;
        //Postfix=Postfix+symbol;
        strcat(Postfix,symbol);
 }


jtshaw 09-27-2005 08:51 AM

A little cat program to show what I mean....


Code:

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

int main() {
    string str1 = "bar";
    string str2 = "foo";
    string str3;

    str3 = str2 + str1;

    cout << str3 << endl;
    return 0;
}

Code:

j_shaw@jshaw ~ $ g++ -Wall -o test test.cpp
j_shaw@jshaw ~ $ ./test
foobar



All times are GMT -5. The time now is 11:09 AM.