question about string at() operation

K

key9

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
 
K

kwikius

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
 
B

BobR

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.
 
G

Greg

key9 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top