C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

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

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
winbatch
Guest





PostPosted: Tue Apr 26, 2005 9:32 am    Post subject: Trying to overload + for class to add to string 'is illegal' Reply with quote



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 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;


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Alberto Barbati
Guest





PostPosted: Tue Apr 26, 2005 5:00 pm    Post subject: Re: Trying to overload + for class to add to string 'is ille Reply with quote



winbatch wrote:
Quote:
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 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();
}

This defines a binary operator+ that takes two Dan operands by
reference. The first Dan object is the implied "this" object, which is
always present in all member functions, "a" is the second operand.

With that function you can write 'dan1 + dan2' and the result would be a
string.

Quote:
snip

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


Here, you are trying to add a string to a Dan, which is completely
different from what you defined above. In order to make this code
compiler you should provide an operator+ that takes as operands a string
and a Dan. Even better, you can provide two of them to emulate
commutativity. For example:

string operator+(const string& s, const Dan& a)
{
return s + a.toString();
}

string operator+(const Dan& a, const string& s)
{
return a.toString() + s;
}

Notice that:

1) those are *free* functions not member functions. They should be
either defined outside class Dan or prefixed with keyword friend.

2) the parameters are taken by const-reference and not by simple
reference, because:

a) it's a const-correctness issue
(http://www.parashift.com/c++-faq-lite/const-correctness.html) This
means you need to also declare toString() as const

b) otherwise you could not write '"Test" + temp' because the string
literal will produce a temporary string and a temporary cannot be bound
to a non-const reference

HTH,

Alberto

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Bronek Kozicki
Guest





PostPosted: Tue Apr 26, 2005 5:04 pm    Post subject: Re: Trying to overload + for class to add to string 'is ille Reply with quote



winbatch <hoffmandan (AT) gmail (DOT) com> wrote:
Quote:
What does this mean and how do I make it legal?


Quote:
Dan temp;
string myString = "Test" + temp;


because your operator+ is member function, type of its left argument
must be of class type. In above expression it is not. There is no
overloaded operator+ taking string literal as its left side parameter
and Dan as right one. In order to define one, make you operator+
non-member function (or static member function) taking appropriate
arguments. Defining implicit conversion operator from string literal (if
it makes sense for your class) will allow you to define just one
operator+; otherwise you will need to define more overloads.

As a side note, I think that you get it wrong, anyway, and class Dan
should not have overloaded operator+ at all. That's because you return
string object, not your class. Which is not what users would expect from
overloaded operator.


B.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Mark Van Peteghem
Guest





PostPosted: Wed Apr 27, 2005 7:55 am    Post subject: Re: Trying to overload + for class to add to string 'is ille Reply with quote

If you have

class A
{
public:
string operator+(const B &b) const
{
...
}
};

you can only do a+b, if a is a A object and b is a B object.
The best thing for what you want to do is to define two non-member
functions as follows:

string operator+(const string &str, const Dan &dan)
{
...
}

string operator+(const Dan &dan, const string &str)
{
...
}

then you can add strings and Dan objects on each side of +.
If necessary, you can make these friends of your class.

--
Mark dot Van dot Peteghem at q-mentum dot com
http://www.q-mentum.com -- easier and more powerful unit testing

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
rwp
Guest





PostPosted: Wed Apr 27, 2005 8:10 am    Post subject: Re: Trying to overload + for class to add to string 'is ille Reply with quote

The operation you are overloading is the following:

Dan dan1, dan2;

string myString = dan1 + dan2;


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Bronek Kozicki
Guest





PostPosted: Wed Apr 27, 2005 8:21 am    Post subject: Re: Trying to overload + for class to add to string 'is ille Reply with quote

Bronek Kozicki wrote:
Quote:
non-member function (or static member function) taking appropriate
^^^^^^^^^^^^^^^^^^^^^^^^

Mistake, sorry about that. Overloaded operator may not be static member
function.


B.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
radu.chindris@gmail.com
Guest





PostPosted: Wed Apr 27, 2005 8:24 am    Post subject: Re: Trying to overload + for class to add to string 'is ille Reply with quote


winbatch wrote:
Quote:
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 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;

You're trying to use the global '+' operator to add to a char * the
value 'temp'. Don't know what you were thinking of, but D::+ is not,
even by far, called on the last line Wink
try this:
string myString = "Test" + temp.toString();

Greets,
RC


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Antoun Kanawati
Guest





PostPosted: Wed Apr 27, 2005 8:35 am    Post subject: Re: Trying to overload + for class to add to string 'is ille Reply with quote

winbatch wrote:
Quote:
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 std::allocator<char>> + Dan" is illegal.

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

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

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

You will need something like this, a non-member function:

const string operator+(const string &a, const Dan &b) { ... }

A member operator+ requires that the first argument be of the class
that contains the operator.

In your example, the first argument is a char*, not a Dan instance.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Jonathan Bartlett
Guest





PostPosted: Wed Apr 27, 2005 8:37 am    Post subject: Re: Trying to overload + for class to add to string 'is ille Reply with quote


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

The "operator +" that you have defined is only for adding Dan's to
Dan's, not for adding Dan's to strings.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Allan W
Guest





PostPosted: Wed Apr 27, 2005 8:40 am    Post subject: Re: Trying to overload + for class to add to string 'is ille Reply with quote

winbatch wrote:
Quote:
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 std::allocator<char>> + Dan" is illegal.

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

Operator+ is usually best as a non-member function. It is legal for
you to do it this way, but this supports only Dan+Dan -- not Dan+string
or string+Dan.

If you had a constructor (without using "explicit") that turned a
char* into a Dan, and if toString() was a const function and the
argument to operator+ was a const reference, then your operator+
could handle Dan+string -- the second argument would automatically
call the Dan constructor. But it still wouldn't work with string+Dan!

Quote:

....

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

Try it this way:

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

class Dan {
public:
string first;
string last;
Dan(string firstname="first", string lastname="last")
: first(firstname), last(lastname) {}
// toString() should be a const function, because
// it does not alter the Dan object at all.
string toString() const { return first + ", " + last; }
};
const string operator+(const Dan&a, const Dan&b) {
return a.toString() + b.toString();
}

int main() {
Dan temp;
string myString = string("Test") + temp;
cout << myString << endl; // "Test, lastfirst, last"
return 0;
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Bo Persson
Guest





PostPosted: Wed Apr 27, 2005 8:45 am    Post subject: Re: Trying to overload + for class to add to string 'is ille Reply with quote


"winbatch" <hoffmandan (AT) gmail (DOT) com> skrev i meddelandet
news:1114452433.455400.141510 (AT) f14g2000cwb (DOT) googlegroups.com...
Quote:
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 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();
}

If this is a member of class Dan, it actually takes 2 parameters, 'this'
and 'a'.

Quote:

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

....


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

This isn't the operator you defined. Try

string myString = Dan + Dan;

and it will compile (but perhaps not do what you wanted).


Bo Persson



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
codigo
Guest





PostPosted: Wed Apr 27, 2005 8:49 am    Post subject: Re: Trying to overload + for class to add to string 'is ille Reply with quote


"winbatch" <hoffmandan (AT) gmail (DOT) com> wrote

Quote:
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 std::allocator<char>> + Dan" is illegal.

Shouldn't Dan be an instance of a Person class? What's a Dan? operator+
shouldn't be a member function, it could be made a "friend". But here that
isn't required since such a friend function doesn't need to access the
class's private internals (toString() does that for you).

Note that both a (string + Person) and (Person + string) are possible. Here
is an example where the output stream operator is defined as well:

#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
using std::ostream;

class Person
{
string first; // private members
string last;
public:
Person(string f, string l) : first(f), last(l)
{
}

~Person() { }

// only needed if these access private members
//friend ostream& operator<<(ostream& os, const Person& p);
//friend const string operator+(string s, Person & p);

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

ostream& operator<<(ostream& os, const Person& p)
{
return os << p.toString();
}

const string operator+(string s, Person& p)
{
return s + p.toString();
}

const string operator+(Person& p, string s)
{
return p.toString() + s;
}

int main(int argc, char* argv[])
{
Person dan("Daniel", "Smith");
Person sue("Sue", "Carter");

cout << dan << " loves " << sue << endl;

string s = dan + " loves " + sue;
cout << s << endl;

return 0;
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.