I want to add a new line in this. This is my sample code:
ui->button->setText(" Tips " + "\n" + TipsCount );
This is the error it shows:
invalid operands of types ‘const char [7]’ and ‘const char [2]’ to binary ‘operator+’
But when I add to label it gets appended!
ui->label->setText(name + "\n" + City );
Can someone please help me?
This is a very common problem in C++ (in general, not just QT).
Thanks to the magic of operator overloading,
name + "\n"gets turned into a method call (couldn’t say which one since you don’t list the type). In other words, because one of the two things is an object with+overloaded it works.However when you try to do
"abc" + "de", it blows up. The reason is because the compiler attempts to add two arrays together. It doesn’t understand that you mean concatenation, and tries to treat it as an arithmetic operation.To correct this, wrap your string literals in the appropriate string object type (
std::stringorQStringmost likely).