From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24596 invoked by alias); 1 Sep 2004 11:07:08 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 24574 invoked from network); 1 Sep 2004 11:07:06 -0000 Received: from unknown (HELO applibk.profile.co.at) (193.83.21.2) by sourceware.org with SMTP; 1 Sep 2004 11:07:06 -0000 Received: from jupiter.profile.co.at (jupiter.profile.co.at [193.83.20.5]) by applibk.profile.co.at (8.12.8/8.12.8) with ESMTP id i81B72xq032238; Wed, 1 Sep 2004 13:07:02 +0200 Received: from [193.83.20.32] (mimas.profile.co.at [193.83.20.32]) (authenticated bits=0) by jupiter.profile.co.at (8.12.10/8.12.10) with ESMTP id i81B71Bk016620; Wed, 1 Sep 2004 13:07:01 +0200 Subject: Re: error: no match for `std::string& != long int' operator From: Arno Wilhelm Reply-To: arno.wilhelm@profile.co.at To: learning c++ Cc: gcc-help@gcc.gnu.org In-Reply-To: References: Content-Type: text/plain Organization: proFILE Computersysteme GmbH Message-Id: <1094036821.3241.4.camel@mimas.profile.co.at> Mime-Version: 1.0 Date: Wed, 01 Sep 2004 11:07:00 -0000 Content-Transfer-Encoding: 7bit X-SW-Source: 2004-09/txt/msg00004.txt.bz2 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 #include 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 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 > #include > > using namespace std; > > int main(){ > > string s1("0123456789abcdefghijklmnopqrstuvwxyz"); > for(s1; s1!=NULL; ++s1) > cout <<*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 proFILE Computersysteme GmbH