473,407 Members | 2,546 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,407 software developers and data experts.

Problem with string parameters

I made this function which given two strings c1 and c2 test if with the
letters of both you can exactly form the string c3, so for exaple c1 =
"evol" c2= "ution" and c3 = "evolution" will yield true.

The function looks like this:

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

bool areSubComp(string c1, string c2, string c3)
{
c1+=c2;
sort(c1.begin(),c1.end());
sort(c3.begin(),c3.end());
if(c1==c3)
return true;
else
return false;
}

int main()
{
cout << areSubComp("eouio","vltn","evolution"); // line 19

return 0;
}

I was trying to pass the last two arguments as reference to avoid the
copy of both strings using:

bool areSubComp(string c1, string& c2, string& c3)

but the compiler gives me this error:

line 19: cannot convert parameter 2 from 'char [5]' to 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > &'

What's wrong and how can I accomplish what I wanted to?

Thanks.

Apr 24 '06 #1
4 2145

Gaijinco wrote:
I was trying to pass the last two arguments as reference to avoid the
copy of both strings using:

bool areSubComp(string c1, string& c2, string& c3)


You are sorting c3. It seems like passing it as a (non-const) reference
is a bad idea. Also, you cannot initialize a (non-const) reference from
a char*.

Try this:

bool areSubComp(const string& c1, const string& c2, string ci)
{
string cp(c1 + c2);

sort(cp.begin(),cp.end());
sort(ci.begin(),ci.end());

return (cp.compare(ci) == 0);
}

Cheers,
Andre

Apr 24 '06 #2

Gaijinco je napisao:
I made this function which given two strings c1 and c2 test if with the
letters of both you can exactly form the string c3, so for exaple c1 =
"evol" c2= "ution" and c3 = "evolution" will yield true.

The function looks like this:

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

bool areSubComp(string c1, string c2, string c3)
{
c1+=c2;
sort(c1.begin(),c1.end());
sort(c3.begin(),c3.end());
if(c1==c3)
return true;
else
return false;
}

int main()
{
cout << areSubComp("eouio","vltn","evolution"); // line 19

return 0;
}

I was trying to pass the last two arguments as reference to avoid the
copy of both strings using:

bool areSubComp(string c1, string& c2, string& c3)

but the compiler gives me this error:

line 19: cannot convert parameter 2 from 'char [5]' to 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > &'

What's wrong and how can I accomplish what I wanted to?

Thanks.


Note that "eouio" is constant string - you can not legaly change
values.

One solution is to make params const:

bool areSubComp(string c1, const string&c2, const string& c3);

Than, compiler will make temprary string. This solution raise new
problem - you can not sort const data! So, you can make new copy into
temp variables, but this will end like starting code (you do not wanna
copy).

If your code do not need speed just leave it - string copy is not so
expensive.

If speed metters: you can use char* whit your or std::qsort sorting

Best,
Zahaije Pasalic

Apr 24 '06 #3
Andre's suggestion is a good way.

because of various library functions, it's better to avoid "+" or
"==".

but to Andre, I have another question,about why 'const'.. like
this(const string& c1)
in order to protect c1 from changing ?

Apr 25 '06 #4
Your function is defined as taking non-const references to std::string:

bool areSubComp(string c1, string& c2, string& c3)

You are passing in const char*:

cout << areSubComp("eouio","vltn","evolution");

std::string is implicitly convertable from const char*, i.e. it has a
non-explicit constructor that takes as it's only parameter a const
char*. This allows you to automagically create a std::string from a
const char*. So, in your case, the compiler basically creates a string
object on the stack from the const char* you are using and passes
_that_ to the function.

However, this std::string object is _temporary_ and that causes a
problem with the non-const reference, because you cannot use a
non-const reference to hold a temporary object. You _can_ use a const
reference, however. So, the simple solution to your problem is to
change your function definition to take const references to
std::string, like so:

bool areSubComp(string c1, const string& c2, const string& c3)

Apr 25 '06 #5

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

Similar topics

5
by: Jason Huang | last post by:
Hi, The SqlParameter myPM =new SqlParameter("@Address", txtAddress.Text) is working for update, but SqlParameter myPM =new SqlParameter ("@Address",SqlDbType.NVarChar,90,txtAddress.Text) is...
1
by: Thanks | last post by:
I have a routine that is called on Page_Init. It retrieves folder records from a database which I display as Link Buttons in a table cell. I set the table cell's bgcolor to a default color (say...
1
by: leslie_tighe | last post by:
Hello, I have webservice created with Axis 1.2.1 and that I am trying to consuming in .NET (VB) using the Microsoft provided tools. While I am able to consume methods on the service that return...
4
by: leslie_tighe | last post by:
Hello, I have a webservice running on a J2EE server created with Axis 1.2.. I have a client that I am building in .net that needs to consume this webserivce and am having a bit of trouble. I have...
0
by: leslie_tighe | last post by:
Hello, I have a web service that is running in a java server using axis that I want to use from .net. In VS.net 2003 I setup a project with a web reference. Using generated code I am able to...
0
by: ryan | last post by:
I've been tasked with consuming a Perl web service that was written by a person in a different department of my company. The problem is it's the guy's first attempt at web services and he doesn't...
2
by: Constantine | last post by:
Hi, I have developed one class called CProductInfo providing == and != operator features. I have one problem. When I use this class in my program, say CProductInfo product = null; and...
3
by: Constantine | last post by:
Hi, I have developed one class called CProductInfo providing == and != operator features. I have one problem. When I use this class in my program, say CProductInfo product = null; and...
5
by: explode | last post by:
I made a procedure Public Sub Novo(ByVal nova1 As String, ByVal nova2 As String) that creates a new oledbDataAdapter with insert update select and delete commads. I also added that commands can...
12
by: Light | last post by:
Hi all, I posted this question in the sqlserver.newusers group but I am not getting any response there so I am going to try it on the fine folks here:). I inherited some legacy ASP codes in my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.