LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-27-2005, 08:20 AM   #1
sajith
Member
 
Registered: Sep 2005
Location: kannur
Posts: 59

Rep: Reputation: 15
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
 
Old 09-27-2005, 08:40 AM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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);
 }
 
Old 09-27-2005, 08:51 AM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
updating new installation of 9.3 ERROR...ERROR...ERROR Morbid SUSE / openSUSE 3 08-15-2005 11:22 PM
Hard drive failure error? (sense key Medium error) Arodef Linux - Hardware 2 11-02-2004 12:39 PM
QMAIL error:554 Transaction failed: message format error mpk25 Linux - Software 1 10-15-2004 12:53 PM
c++ ostream error, messag rot large to trakc down location of error. exodist Programming 2 06-13-2004 06:35 PM
cdrecord inupt/output error. write_g1: scsi sendcmd: no error lucstuk Linux - Software 9 08-09-2003 09:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration