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_str ing<char, std::char_trait s<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; 5 2055
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_str ing<char, std::char_trait s<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
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.toStri ng();
return tryme;
}
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.toStri ng(); 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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 };
template <class A, class B>
|
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 an array of pointer to Attore.
I have written these overload in these ways. the overload of << work
correctly the overload of >> I don't know. I compile the class correctly,
when I insert a film through the operator >> I don't receive error, but...
|
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:
public static bool operator !=(MyType x, MyType y)
{
return !(x == y);
}
That way the == operator handles everything, and extra comparing logic isn't
|
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 overload resolution ?
I've used parameters of type object and string here, just to illustrate the problem. Really I have a bit more deep inheritance graph, and the things get more interesting if the strongly typed overload is like override public void...
|
by: shachar |
last post by:
hi all.
can i OverLoad Operators - such as Exclamation Mark "!" ?
how?
| |
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 code but can't seem to see the
error. I don't understand a lot of the fancy stuff so if you can keep it
simple for me!!!! Any help is appreciated!
Here is the code:
|
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 attempt to check to
see if the object is null:
"The call is ambiguous between the following methods or properties:
'TestObject.operator ==(TestObject, string)' and 'TestObject.operator
==(TestObject, TestObject)"
Does anyone have any idea how to...
|
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;
|
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 long long int), a decimal (double), or a string (std::string). I basically want to overload most of a Message's operators so that the Message class feels more like a native type. For example...
Message msg = "tester";
std::string str = msg;...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |