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

W

winbatch

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

Victor Bazarov

winbatch said:
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
 
W

winbatch

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

Victor Bazarov

winbatch said:
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
 
W

winbatch

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
 
V

Victor Bazarov

winbatch said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top