public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* error: no match for `std::string& != long int' operator
@ 2004-09-01 10:22 learning c++
  2004-09-01 11:07 ` Arno Wilhelm
  0 siblings, 1 reply; 3+ messages in thread
From: learning c++ @ 2004-09-01 10:22 UTC (permalink / raw)
  To: gcc-help

Hi,
I have a short code hope to print the character of a string one by one. but 
there are some errors.

if I declare srting s1, is s1 a pointer? are there some difference between C 
and C++?
#include <iostream>
#include <string>

using namespace std;

int main(){

string s1("0123456789abcdefghijklmnopqrstuvwxyz");
for(s1; s1!=NULL; ++s1)
cout <<*s1<<" " <<int(*s1)<<"  ";

return 0;

}
printc.cpp: In function `int main()':
printc.cpp:9: error: no match for `std::string& != long int' operator
printc.cpp:9: error: no match for `++ std::string&' operator
printc.cpp:10: error: no match for `* std::string&' operator
printc.cpp:10: error: no match for `* std::string&' operator
printc.cpp:9: warning: statement with no effect

_________________________________________________________________
The new MSN 8: advanced junk mail protection and 2 months FREE* 
http://join.msn.com/?page=features/junkmail

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: error: no match for `std::string& != long int' operator
  2004-09-01 10:22 error: no match for `std::string& != long int' operator learning c++
@ 2004-09-01 11:07 ` Arno Wilhelm
  0 siblings, 0 replies; 3+ messages in thread
From: Arno Wilhelm @ 2004-09-01 11:07 UTC (permalink / raw)
  To: learning c++; +Cc: gcc-help

It seems that you are mixing up C-Strings with std::string.
Std::string is an object and you need an iterator to "walk" through it:


// compile with: g++ -W -Wall -g -o std_string stdstring.cpp

#include <iostream>
#include <string>

using namespace std;

int main(void)
{
        string s1("0123456789abcdefghijklmnopqrstuvwxyz");

        std::string::const_iterator iter = s1.begin();
        for( ; iter != s1.end(); ++iter )
                {
                cout << *iter << " " << int(*iter) << endl;
                }

return 0;
}

A C-String is a pointer to the beginning of an 0 terminated array of
chars. If you want to do the same with an C-String you would have to use
this code:

// compile with: g++ -W -Wall -g -o c_string cstring.cpp

#include <iostream>

using namespace std;

int main(void)
{
        char * s1 = "0123456789abcdefghijklmnopqrstuvwxyz";

        for( ; *s1 != 0x00; ++s1 )
                {
                cout << *s1 << " " << int(*s1) << endl;
                }

return 0;
}

Both of this programms give you the following output:

> ./c_string
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
a 97
b 98
c 99
d 100
e 101
f 102
g 103
h 104
i 105
j 106
k 107
l 108
m 109
n 110
o 111
p 112
q 113
r 114
s 115
t 116
u 117
v 118
w 119
x 120
y 121
z 122


Hope this helps,

Arno




> Hi,
> I have a short code hope to print the character of a string one by one. but 
> there are some errors.
> 
> if I declare srting s1, is s1 a pointer? are there some difference between C 
> and C++?
> #include <iostream>
> #include <string>
> 
> using namespace std;
> 
> int main(){
> 
> string s1("0123456789abcdefghijklmnopqrstuvwxyz");
> for(s1; s1!=NULL; ++s1)
> cout <<*s1<<" " <<int(*s1)<<"  ";
> 
> return 0;
> 
> }
> printc.cpp: In function `int main()':
> printc.cpp:9: error: no match for `std::string& != long int' operator
> printc.cpp:9: error: no match for `++ std::string&' operator
> printc.cpp:10: error: no match for `* std::string&' operator
> printc.cpp:10: error: no match for `* std::string&' operator
> printc.cpp:9: warning: statement with no effect
> 
> _________________________________________________________________
> The new MSN 8: advanced junk mail protection and 2 months FREE* 
> http://join.msn.com/?page=features/junkmail
-- 
Arno Wilhelm <arno.wilhelm@profile.co.at>
proFILE Computersysteme GmbH

^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: error: no match for `std::string& != long int' operator
@ 2004-09-01 10:38 Lev Assinovsky
  0 siblings, 0 replies; 3+ messages in thread
From: Lev Assinovsky @ 2004-09-01 10:38 UTC (permalink / raw)
  To: learning c++, gcc-help

Sorry, but you code is wrong.
string is a class name and s1 is an object.
You can't apply != NULL or ++ unless 
these operators are declared by basic_string.
As far as I know basic_string doesn't have those operators.

-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org]On
Behalf Of learning c++
Sent: Wednesday, September 01, 2004 2:23 PM
To: gcc-help@gcc.gnu.org
Subject: error: no match for `std::string& != long int' operator


Hi,
I have a short code hope to print the character of a string one by one. but 
there are some errors.

if I declare srting s1, is s1 a pointer? are there some difference between C 
and C++?
#include <iostream>
#include <string>

using namespace std;

int main(){

string s1("0123456789abcdefghijklmnopqrstuvwxyz");
for(s1; s1!=NULL; ++s1)
cout <<*s1<<" " <<int(*s1)<<"  ";

return 0;

}
printc.cpp: In function `int main()':
printc.cpp:9: error: no match for `std::string& != long int' operator
printc.cpp:9: error: no match for `++ std::string&' operator
printc.cpp:10: error: no match for `* std::string&' operator
printc.cpp:10: error: no match for `* std::string&' operator
printc.cpp:9: warning: statement with no effect

_________________________________________________________________
The new MSN 8: advanced junk mail protection and 2 months FREE* 
http://join.msn.com/?page=features/junkmail

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-09-01 11:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-01 10:22 error: no match for `std::string& != long int' operator learning c++
2004-09-01 11:07 ` Arno Wilhelm
2004-09-01 10:38 Lev Assinovsky

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).