This is going to sound crazey, but I've no idea why this 'wont work'.
x and y are int's. The end result has to be a char.Code:message = "value of x is " << x << " value of y is " << y;
error C2296: '<<' : illegal, left operand has type 'char [14]'
This is a discussion on Join string and int. within the C++ Programming forums, part of the General Programming Boards category; This is going to sound crazey, but I've no idea why this 'wont work'. Code: message = "value of x ...
This is going to sound crazey, but I've no idea why this 'wont work'.
x and y are int's. The end result has to be a char.Code:message = "value of x is " << x << " value of y is " << y;
error C2296: '<<' : illegal, left operand has type 'char [14]'
Hi,
It looks like you took a statement like this:
and substituted message= in place of cout<<. What on earth did you read about C++ that made you think that cout<< and message= are equivalent?Code:cout<<"value of x is " << x << " value of y is " << y;
The fact that you are asking such a question indicates you probably wouldn't understand the somewhat complex method you have to use for joining a string and an int in C++, but if you are interested look up stringstreams.
Last edited by 7stud; 06-11-2005 at 03:48 AM.
'message' is a char variable.
Where x is 5, and y were 3, I'd like the string to read
I assumed that << would join variables together. In php, you would write something like:value of x is 5 value of y is 3.
Might I have some example code equivilent to that?Code:echo "value of x is " . $x . " value of y ix " . $y;
>>>> EDIT
I've solved the issue, is this a hacky solution?
Code:int x = 5, y = 3; char message [50]; sprintf( message, "x is %i , y is %i ", x, y);
Last edited by xconspirisist; 06-11-2005 at 04:34 AM.
operator << can take string and numeric right-hand arguments, but there must be a class on the left-hand side performing the operation (whatever that operation may be). In the example ofit would be equivalent to writingCode:cout << "the value of x is " << x;Don't confuse that dot with a PHP dot for concatenation. cout's operator<< implementation returns what you should think of as cout itself. So really that translation would also be equivalent to sayingCode:cout.operator<<("the value of x is ").operator<<(x);Since you are posting in the c++ thread, a grand solution would be to take advantage of the ostringstream class (#include <sstream>). You can search the board for examples.Code:cout.operator<<("the value of x is"); cout.operator<<(x);
For a more primitive solution, use sprintf().
Last edited by anonytmouse; 06-11-2005 at 04:55 AM.
basicly you haveOriginally Posted by xconspirisist
primitive type A << primitive type B
in C/C++, this operator, for primitive types, is only defined for integers (obviously), because it's a bitwise operator.
So char[] <<int doesn't exist, and one can only overload an operator that has at least one class as argument.
So, bad luck.
What you what is this
Yes I separated in two lines because char* + int is char* but pointing wherelseCode:string message("value of x is "); message = message + x +" value of y is " +y;
>> What you what is this
I seriously doubt that is what the OP wanted. Using the operator+ on an int with a string does not add the string representation of that int to the string. A stringstream does that (as has been mentioned already). Instead, you will probably get an error, and if it did work it would add on the character whose code is equal to that int.