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

Odd behaviour of operator+()

Consider this:

#include <iostream>
#include <string>

std::string operator+(const char* a, const std::string& b)
{
return std::string(a)+b;
}

int main()
{
std::string s = "abc" + "def";
std::cout << s << std::endl;
}

With gcc 3.3.5 it gives the rather strange error:

test.cc:11: error: invalid operands of types `const char[4]' and `const
char[4]
' to binary `operator+'

If I substitute the offending line with this:

std::string s = "abc" + std::string("def");

or even with this:

std::string s = operator+("abc", "def");

then it works just ok. However, as given above it doesn't. How come?
(References to the C++ standard preferred.)
Jun 24 '06 #1
3 1409
Hi,
there is no instance of operator+(const char *,const char *).
try this :

std::string operator+(const char* a, const char *b)
{
return std::string(a)+std::string(b);
}

I don't know why "std::string s = operator+("abc", "def");" works !?!
Regards
Thorsten

Jun 24 '06 #2
On Sat, 24 Jun 2006 21:25:45 +0300, Juha Nieminen
<no****@thanks.invalid> wrote:
Consider this:

#include <iostream>
#include <string>

std::string operator+(const char* a, const std::string& b)


BTW, this function is already implemented by std::(basic_)string.
Jun 24 '06 #3
* Juha Nieminen:
Consider this:

#include <iostream>
#include <string>

std::string operator+(const char* a, const std::string& b)
{
return std::string(a)+b;
}

int main()
{
std::string s = "abc" + "def";
std::cout << s << std::endl;
}


You can overload an operator on class or enum type. You can not
overload the built-in operators. The usage

"abc" + "def"

invokes the built-in operator+, with invalid arguments.

Ignore the replies that indicate you can overload the built-in operator.

You can't.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 24 '06 #4

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

Similar topics

45
by: Jordan Rastrick | last post by:
Can anybody please give me a decent justification for this: class A(object): def __init__(self, a): self.a = a def __eq__(self, other): return self.a == other.a s = A(3)
2
by: Daniel Schüle | last post by:
Hi all! given the following code #include <iostream> using std::cout; using std::endl; struct X
4
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently...
13
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int...
3
by: Carlo Capelli | last post by:
I found a change in the following code, that behaved correctly in VC++ 6. #include <strstream> using namespace std; void main() { char x; ostrstream(x, 100) << "pippo" << "pluto" << ends;...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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
isladogs
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...

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.