473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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<chara€?
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


Nov 25 '06 #1
3 1621
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

Nov 25 '06 #2

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
Nov 25 '06 #3
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

Nov 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Shawn Meyer | last post by:
Hello - I am trying to write a class that has an async BeginX and EndX, plus the regular X syncronous method. Delegates seemed like the way to go, however, I still am having problems getting...
13
by: John | last post by:
I've got some reasonably complex business logic in my C# code, in a class called by a ASP.NET page. This takes around 3-4 seconds to execute. It's not dependent on SQL calls or anything like that....
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
1
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
29
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
2
by: Terrance | last post by:
Hello, I have a question in regards to hashtables that I was hoping someone can share some light on. I'm currently working with VB.net and I'm trying to learn as much as possible about the...
0
by: robert | last post by:
Hi all, I'm having a hard time resolving a namespace issue in my wsdl. Here's an element that explains my question, with the full wsdl below: <definitions name="MaragatoService"...
19
by: the other john | last post by:
I'm trying to enhance a script to do the following things. 1). detect the first four words of a paragraph and stylize them (already does that) 2). Capitalize and stylize the first letter of each...
0
by: ChopStickr | last post by:
I have a custom control that is embedded (using the object tag) in an html document. The control takes a path to a local client ini file. Reads the file. Executes the program specified in...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.