How come operator+=
is defined for std::string
but operator+
is not defined? See my MWE below (http://ideone.com/OWQsJk).
#include <iostream>
#include <string>
using namespace std;
int main() {
string first;
first = "Day";
first += "number";
cout << "\nfirst = " << first << endl;
string second;
//second = "abc" + "def"; // This won't compile
cout << "\nsecond = " << second << endl;
return 0;
}
You need to convert one of the raw string literals to std::string
explicitly. You can do it like others already mentioned:
second = std::string("abc") + "def";
or with C++14, you will be able to use
using namespace std::literals;
second = "abc"s + "def";
// note ^
Those aren't std::string
s, they are const char *
. Try this:
second = std::string("abc") + "def";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With