473,382 Members | 1,447 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,382 software developers and data experts.

Trying to overload + for class to add to string 'is illegal'

Hi,

I'm trying to write code such that my class can be used like so:
To accomplish this,
I'm trying to overload the + operator of my class to return a string. I
am able to do this, but when I try and do the operation above, I get:
Error: The operation "std::basic_string<char, std::char_traits<char>,
std::allocator<char>> + Dan" is illegal.

What does this mean and how do I make it legal?

class Dan
{
public:
string first;
string last;
const string operator + (Dan & a)
{
return a.toString();
}

string toString()
{
return first + ", " + last;
}

.....
Dan temp;
string myString = "Test" + temp;

Jul 23 '05 #1
5 2028
winbatch wrote:
I'm trying to write code such that my class can be used like so:
To accomplish this,
I'm trying to overload the + operator of my class to return a string. I
am able to do this, but when I try and do the operation above, I get:
Error: The operation "std::basic_string<char, std::char_traits<char>,
std::allocator<char>> + Dan" is illegal.

What does this mean and how do I make it legal?

class Dan
{
public:
string first;
string last;
const string operator + (Dan & a)
{
return a.toString();
}

string toString()
{
return first + ", " + last;
}

....
Dan temp;
string myString = "Test" + temp;


The problem is that you're trying to add your object to a literal,
which has the type "an array of const char". To achieve that you
need to define a non-member operator+ function which will have
'const char*' as its first argument and 'Dan const&' as its second
argument.

V
Jul 23 '05 #2
Victor,

I'm not sure I understand what you mean by non-member - do you mean not
inside the Dan class?

Is this at all what you mean?

const string operator + (const char* test, Dan & a)
{
string tryme=test;
tryme+=a.toString();
return tryme;
}

Jul 23 '05 #3
winbatch wrote:
I'm not sure I understand what you mean by non-member - do you mean not
inside the Dan class?
That's right.

Is this at all what you mean?

const string operator + (const char* test, Dan & a)
I think it should be

string operator + (const char* test, Dan const & a)

or

string operator + (const string& test, Dan const & a)

(why return a const string? why pass a non-const 'Dan'?)
{
string tryme=test;
tryme+=a.toString();
return tryme;
(a) Make sure 'toString' is declared 'const' to be able to use it
with a const 'Dan' object.

(b) Just write

return test + a.toString();

There is no need in three statements when one is sufficient.
}


V
Jul 23 '05 #4
Victor,
Thanks for the advice. I got it to work by putting it outside the
class. I'm confused though, is there no way to get it to work inside
the class?

Dan

Jul 23 '05 #5
winbatch wrote:
Thanks for the advice. I got it to work by putting it outside the
class. I'm confused though, is there no way to get it to work inside
the class?


Yes, but you need to create a non-explicit constructor from 'const char*'.
Read more about operator overloading in "Effective C++" and other good
books.

V
Jul 23 '05 #6

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

Similar topics

9
by: Alexander Stippler | last post by:
Hi, I have got a question concerning the overload resolution rules of C++ and enumeration types. The following little test program illustrates the situation: enum { red, green, blue }; ...
3
by: Piotre Ugrumov | last post by:
I have done the overload on the operator >> and << in the class Attore. These 2 overload work correctly. I have done the overload of the same overload in the class Film. The class film ha inside...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
13
by: Vladimir Granitsky | last post by:
Hi guys, Please, look at the code below and try to step into it. The compiled code calls the loosely typed method public void Method1(object o) !?!? Am I right that C# compiler does wrong...
10
by: shachar | last post by:
hi all. can i OverLoad Operators - such as Exclamation Mark "!" ? how?
11
by: DrNoose | last post by:
Hi! I've got a program that's almost done, but I'm getting compile errors in two lines: 317 & 319. I think the main error has to do with the Truck Class. I'm a newbie and keep looking at the...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
4
by: sandy | last post by:
I am trying to upper case a string, so I have this method: string FileSystem::toupper(string S) { for (int i=0; i<S.length(); ++i) { S=toupper(S); } return S;
3
by: i3x171um | last post by:
To start off, I'm using GCC4. Specifically, the MingW (setjmp/longjmp) build of GCC 4.2.1 on Windows XP x64. I'm writing a class that abstracts a message, which can be either an integer (stored as...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.