question about string at() operation



 DEVELOP > c-Plus-Plus > question about string at() operation

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "key9"
Date: 25 Nov 2006 01:26:12 AM
Object: question about string at() operation
Hi all
on coding I met such a problem : which alike this code
#include<iostream>
#include<string>
using namespace std;
string& pstr2(const string& istr){
return istr
}
void pstr(const string& istr){
std::string::size_type sz_;
std::string::size_type sz_E;
sz_E = istr.length();
for (sz_ = 0; sz_ != sz_E ; sz_++){
cout << pstr2(istr.at(sz_)) << endl;
}
}
int main(int argc, char* argv[])
{
string str = " This is test sample";
pstr(str);
return 0;
}
you can not change pstr2() pstr1()'s parameter
$ g++ -o test1 test.cpp
test.cpp: In function a€?std::string& pstr2(const std::string&)a€?:
test.cpp:8: error: invalid initialization of reference of type
a€?std::string&a€? from expression of type a€?const
std::basic_string<char, std::char_traits<char>, std::allocator<char> >a€?
test.cpp:10: error: expected a€?;a€? before a€?}a€? token
test.cpp: In function a€?void pstr(const std::string&)a€?:
test.cpp:21: error: invalid conversion from a€?const chara€? to a€?const
char*a€?
test.cpp:21: error: initializing argument 1 of
a€?std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*,
const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc
= std::allocator<char>]a€?
how to solve it?
thank you very much
your
key9
.

User: "BobR"

Title: Re: question about string at() operation 25 Nov 2006 02:21:58 AM
key9 wrote in message ...

Hi all
on coding I met such a problem : which alike this code

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

string& pstr2(const string& istr){
return istr
}

void pstr(const string& istr){
std::string::size_type sz_;
std::string::size_type sz_E;
sz_E = istr.length();

for (sz_ = 0; sz_ != sz_E ; sz_++){
cout << pstr2(istr.at(sz_)) << endl;
}

Try this:
for(std::size_t i(0); i < istr.size(); ++i){
cout << istr.at( i ) << std::endl;
} // for(i)
look at the output.

}

void pstr( const string& istr ){
for(std::size_t i(0); i < istr.size(); ++i){
std::string tmp;
tmp.push_back( istr.at( i ) );
cout << pstr2( tmp ) << std::endl;
} // for(i)
}


int main(int argc, char* argv[]){
string str = " This is test sample";

pstr(str);
return 0;
}

you can not change pstr2() pstr1()'s parameter

pstr2() wants a std::string, pstr()'s std::string.at() is trying to pass a
char.
--
Bob R
POVrookie
.

User: "kwikius"

Title: Re: question about string at() operation 25 Nov 2006 02:06:42 AM
key9 wrote:
<...>

how to solve it?

various ways...
#include<iostream>
#include<string>
using namespace std;
// return type invalid as istr is const
//also no ';' after istr
//string& pstr2(const string& istr){
// return istr
//}
//change above to...
const string& pstr2(const string& istr){
return istr;
}
void pstr(const string& istr){
std::string::size_type sz_;
std::string::size_type sz_E;
sz_E = istr.length();
for (sz_ = 0; sz_ != sz_E ; sz_++){
// problem in original is that there is
// no constructor for std::string(char)
// std::string t = istr.at(sz_); Error
// so...
#if (1)
//either assign the char t a std:string...
std::string temp_str;
temp_str = istr.at(sz_); // assignment not construction
#else
// or .. make a C-style string
char temp_str[2];
temp_str[1] = '\0'; // dont forget the terminator
temp_str[0] = istr.at(sz_);
#endif
cout << pstr2(temp_str) << endl;
}
}
// or use std::string::substr...
void alt_pstr(const string& istr){
for (std::string::size_type sz_ = 0,sz_E = istr.length();
sz_ != sz_E ;
++sz_){
cout << pstr2(istr.substr(sz_,1)) << endl;
}
}
int main(int argc, char* argv[])
{
string str = " This is test sample";
alt_pstr(str);
return 0;
}
regards
Andy Little
.

User: "Greg"

Title: Re: question about string at() operation 25 Nov 2006 06:53:08 AM
key9 wrote:

Hi all

on coding I met such a problem : which alike this code


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

string& pstr2(const string& istr){
return istr

}

It's not legal to convert a const reference (the istr parameter) into a
non-const reference (the value returned by pstr2()). One solution would
be to overload pstr2() with const and non-const variations:
const string& pstr2( const string& istr);
string& pstr2( string& istr);
Otherwise pstr2() will have difficulty returning a reference and would
likely have to return its result by value instead:
string pstr2( const string& istr)
{
return string(istr);
}
Greg
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER