 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
key9 Guest
|
Posted: Sat Nov 25, 2006 10:11 am Post subject: 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 |
|
| Back to top |
|
 |
BobR Guest
|
Posted: Sat Nov 25, 2006 10:11 am Post subject: Re: question about string at() operation |
|
|
key9 wrote in message ...
| Quote: | 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)
}
| Quote: |
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 |
|
| Back to top |
|
 |
kwikius Guest
|
Posted: Sat Nov 25, 2006 10:11 am Post subject: Re: question about string at() operation |
|
|
key9 wrote:
<...>
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 |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|