error C2110: '+' : cannot add two pointers

Oct 24, 2011 at 2:23am
HI writing a simple payroll output program with Visual Studio 2010... I get the C2110 error while trying to add strings without assigning them to a string object (would be to complicated for the purpose of the program). Here's the piece of the code where I get the error:

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;
...
const string AFFICHAGE_TYPE_EMPLOYE_REGULIER = "R" + char(130) + "gulier";
...
int main(void)
{
...
cout << fixed << setprecision(2)
...
<< setw(30) << "Imp" + char(147) + "t:" << setw(10) << deductionImpot << " $\n"
...
<< setw(30) << "Total des d" + char(130) + "ductions:" << setw(10) << totalDeductions << " $\n\n"
...
}

the point is to insert special ascii characters, because the program is in french, and to fit the resulting word (ex.: "déductions:") in a single setw()... like if I did what I would normally do,

setw(30) << "Total des d" << char(130) << "ductions:"

the setw() would only apply to the first string, so I would have to use multiple setw().

Now I understand that "strings" in this form are pointers and not string objects and that you can't add pointers, and also I've seen in the reference section that you need at least one string object to use the + operator with strings, so my question is, is there a simple way to do the job without having to declare a string object and insert it in my output, which is obviously what I will have to do if there's no easier way.

Thank you!

AeonFLux1212
Oct 24, 2011 at 2:28am
There are 2 things you can do:

1) Forget about adding with the + operator and just output it all with the << operator:

1
2
...
setw(30) << "Imp" << char(147) << "t:" << ...  // no need for +, just use << 


2) Make a temporary string object:

1
2
...
setw(30) << ( string("Imp") + char(147) + "t:" ) << ...



EDIT: I just realized #1 probably wouldn't work because it would mess with your setw. So I guess #2 is the option.
Last edited on Oct 24, 2011 at 2:29am
Oct 24, 2011 at 2:31am
gee that was fast :-) !!! I'll check it out...

EDIT: option 2) is working great...

Thanks a lot!!

AeonFlux1212
Last edited on Oct 24, 2011 at 2:34am
Topic archived. No new replies allowed.