Error: Invalid Operands of Types 'Const Char [35]' and 'Const Char [2]' to Binary 'Operator+'

Error: invalid operands of types ‘const char [35]’ and ‘const char [2]’ to binary ‘operator+’

Consider this:

std::string str = "Hello " + "world"; // bad!

Both the rhs and the lhs for operator + are char*s. There is no definition of operator + that takes two char*s (in fact, the language doesn't permit you to write one). As a result, on my compiler this produces a "cannot add two pointers" error (yours apparently phrases things in terms of arrays, but it's the same problem).

Now consider this:

std::string str = "Hello " + std::string("world"); // ok

There is a definition of operator + that takes a const char* as the lhs and a std::string as the rhs, so now everyone is happy.

You can extend this to as long a concatenation chain as you like. It can get messy, though. For example:

std::string str = "Hello " + "there " + std::string("world"); // no good!

This doesn't work because you are trying to + two char*s before the lhs has been converted to std::string. But this is fine:

std::string str = std::string("Hello ") + "there " + "world"; // ok

Because once you've converted to std::string, you can + as many additional char*s as you want.

If that's still confusing, it may help to add some brackets to highlight the associativity rules and then replace the variable names with their types:

((std::string("Hello ") + "there ") + "world");
((string + char*) + char*)

The first step is to call string operator+(string, char*), which is defined in the standard library. Replacing those two operands with their result gives:

((string) + char*)

Which is exactly what we just did, and which is still legal. But try the same thing with:

((char* + char*) + string)

And you're stuck, because the first operation tries to add two char*s.

Moral of the story: If you want to be sure a concatenation chain will work, just make sure one of the first two arguments is explicitly of type std::string.

ERROR! invalid operands of types 'float' and 'const char [2]' to binary 'operator '

check your parentheses:

cout<<"Addition of Given Fraction is: ("<<N1<<"/"<<D1<<") + ("<<N2<<"/"<<D2<<") = "
<<(((N1*D2)+(N2*D1))<<"/"<<(D1*D2))<<" = "<<(((N1*D2)+(N2*D1))/(D1*D2))<<endl;
^ ^

Try this:

    case '+':
cout<<"Addition of Given Fraction is: ("<<N1<<"/"<<D1<<") + ("<<N2<<"/"<<D2<<") = "<<((N1*D2)+(N2*D1))<<"/"<<(D1*D2)<<" = "<<(((N1*D2)+(N2*D1))/(D1*D2))<<endl;
break;

case '-':
cout<<"Subtraction of Given Fraction is: ("<<N1<<"/"<<D1<<") - ("<<N2<<"/"<<D2<<") = "<<((N1*D2)-(N2*D1))<<"/"<<(D1*D2)<<" = "<<(((N1*D2)-(N2*D1))/(D1*D2))<<endl;
break;

case '*':
cout<<"Multiplication of Given Fraction is: ("<<N1<<"/"<<D1<<") * ("<<N2<<"/"<<D2<<") = "<<(N1*N2)<<"/"<<(D1*D2)<<" = "<<((N1*N2)/(D1*D2))<<endl;
break;

case '/':
cout<<"Division of Given Fraction is: ("<<N1<<"/"<<D1<<") / ("<<N2<<"/"<<D2<<") = "<<(N1*D2)<<"/"<<(D1*N2)<<" = "<<((N1*D2)/(D1*N2))<<endl;
break;

invalid operands of types 'int' and 'const char [11]' to binary 'operator '

Because of operator precedence rules (<< is evaluated before += and -=), you need to bracket your arithmetic expressions, like this:

if ((groupNumber1 == 1))
{
cout<<"You now have a total of "<<"$"<< (initialMoney += teensMoney) <<" and have "<< (initialTime -= teensTime) <<" minutes remaining."<<endl;
}
else if ((groupNumber1 == 2))
{
cout<<"You now have a total of "<<"$"<< (initialMoney += familyMoney) <<" and have "<<(initialTime -= familyTime) <<" minutes remaining."<<endl;
}

invalid operands of types 'const char*' and const char[4]' to binary 'operator+'

Like the error message says, you can't add pointers and arrays

The problematic part is:

"INSERT ..." + id_journal + "','"

Here the literal string "INSERT ..." will decay to a pointer (const char*) and then the value of id_journal is added. This result in the pointer &(("INSERT ...")[id_journal])). In other words, the value of id_journal is used as an array index instead of being converted to a string.

You then try to add this pointer to the literal string "','" which is really a constant array of four characters (including the string null-terminator).

There no usable + operator which can handle this.

The simplest solution is to turn at least one of the operands of the first addition to a std::string object. I suggest the integer variable id_journal since you can't concatenate strings with integers (there's no automatic conversion here):

string insert_query = "INSERT ...VALUES ('" + std::to_string(id_journal) + "','" + ...;

This works because there is an overloaded + operator which takes a const char* on the left-hand side and a std::string on the right-hand side. Then once this is done, you have a std::string object which can be used for any further concatenations.

c++ invalid operands of types 'char*' and 'const char [2]' to binary 'operator+'

This is because there is no operator+ for char* (the return-type of your function) and const char[2] (the type of "\n"), and since you cannot overload operators for built-in types, there cannot be one. Since this question is tagged C++:

Just use std::string instead of char*, all your problems are solved already. std::string will be superior to the hacks you try to do.

Here you can find a overview over strings features and examples how to use them. You then can concatenate strings a,b,c like std::string new_string = a + b + c;

C++: error: invalid operands of types 'const char*' and 'const char [28]' to binary 'operator+'

The line

cout << "Type '1' for checking whether your number, " + a +
", is a number divisible by," + favNum + "." << endl;

Does not work since:

"Type '1' for checking whether your number, " + a does not do what you are hoping it will do.

That line is equivalent to:

const char* cp1 = "Type '1' for checking whether your number, ";
const char* cp2 = cp1 + a; // Depending on the value of a, cp2 points
// to something in the middle of cp1 or something
// beyond.
cout << cp2 + ", is a number divisible by," + favNum + "." << endl;

That is a problem since the plus operator is not defined for the type of cp2 and the string literal that follows the + operator. The error message from the compiler refers to that term.

Type of cp2 is const const*.

Type of the string literal is const char[28].

You can get what you want by using the insertion operator (<<) repeatedly.

cout
<< "Type '1' for checking whether your number, "
<< a
<< ", is a number divisible by,"
<< favNum
<< "."
<< endl;

Make sure to make similar changes to other lines that suffer from the same problem.



Related Topics



Leave a reply



Submit