-
September 10th, 2013, 01:41 PM
#46
Re: how can i convert write() procedure to cout?
Originally Posted by Cambalinho
(the error stills be when i use these function)?
What is the exact error?
Second, the cout is overloaded to take a null-terminated char*. Why not write a simple program to show this?
Code:
#include <iostream>
using namespace std;
int main()
{
char *p = "Hello World";
cout << p;
}
Code:
Output:
Hello World
So what are you trying to achieve? The streams are overloaded already for char*, and if <string> is included, for std::string and std::wstring.
Regards,
Paul McKenzie
-
September 10th, 2013, 01:57 PM
#47
Re: how can i convert write() procedure to cout?
Originally Posted by Paul McKenzie
What is the exact error?
Second, the cout is overloaded to take a null-terminated char*. Why not write a simple program to show this?
Code:
#include <iostream>
using namespace std;
int main()
{
char *p = "Hello World";
cout << p;
}
Code:
Output:
Hello World
So what are you trying to achieve? The streams are overloaded already for char*, and if <string> is included, for std::string and std::wstring.
Regards,
Paul McKenzie
my objective is my function write() accept '+' for concat the literal strings\variables(directly in function). like these(without any function adicional):
Code:
Console a;
a.write("hello " + "world");
and
Code:
Console a;
string b="world";
a.write("hello " + b);
like you see, i don't use any function inside of my function
is my objective.
the error message that i recive:
"C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|10|error: invalid operands of types 'const char [7]' and 'const char [6]' to binary 'operator+'|"
(you see the '7' and '6', i know that depends in char array size(the '\0' is ignored in size))
Last edited by Cambalinho; September 10th, 2013 at 02:10 PM.
-
September 10th, 2013, 02:10 PM
#48
Re: how can i convert write() procedure to cout?
Originally Posted by Cambalinho
my objective is my function write() accept '+' for concat the literal strings\variables(directly in function). like these(without any function adicional):
Good luck, since no one else in the C++ world has done this.
There is a reason why you cannot concatentate string literals -- it is because they are pointers. You cannot take two string literals and concatenate them with "+".
The "+" when it sees a pointer on the left and right hand side does what it always did, even back in the days of K&R C. It takes the pointer value and adds them up, giving a pointer value.
Again, if you need proof, forget about all of the C++ 11 syntax and write a simple C++ program that tries to do what you say you want to do. You will easily see that it is impossible.
Code:
#include <string>
void foo(std::string& s)
{
}
int main()
{
foo("hello" + "world");
}
So, go ahead and try and make the call to foo() work.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; September 10th, 2013 at 02:13 PM.
-
September 10th, 2013, 02:13 PM
#49
Re: how can i convert write() procedure to cout?
Originally Posted by Paul McKenzie
Good luck, since no one else in the C++ world has done this.
There is a reason why you cannot concatentate string literals -- it is because they are pointers. You cannot take two string literals and concatenate them with "+".
The "+" when it sees a pointer on the left and right hand side does what it always did, even back in the days of K&R C. It takes the pointer value and adds them up, giving a pointer value.
Again, if you need proof, forget about all of the C++ 11 syntax and write a simple C++ program that tries to do what you say you want to do. You will easily see that it is impossible.
Regards,
Paul McKenzie
thanks for the information
-
September 10th, 2013, 02:16 PM
#50
Re: how can i convert write() procedure to cout?
Originally Posted by Cambalinho
my objective is my function write() accept '+' for concat the literal strings\variables(directly in function). like these(without any function adicional):
Also, if this is for your custom "language" that you say you are trying to write, then again, trying to get C++ to look like your custom language is not possible in these cases.
The binary + operator cannot be overloaded if two pointers exist on both sides of the operator, plain and simple.
Regards,
Paul McKenzie
-
September 10th, 2013, 02:19 PM
#51
Re: how can i convert write() procedure to cout?
Originally Posted by Paul McKenzie
Also, if this is for your custom "language" that you say you are trying to write, then again, trying to get C++ to look like your custom language is not possible in these cases.
The binary + operator cannot be overloaded if two pointers exist on both sides of the operator, plain and simple.
Regards,
Paul McKenzie
but i can convert:
a.write("hello " + "world")
for:
a.write((string)"hello " + "world");
thanks for all
-
September 10th, 2013, 03:06 PM
#52
Re: how can i convert write() procedure to cout?
Originally Posted by Cambalinho
but i can convert:
a.write("hello " + "world")
for:
a.write((string)"hello " + "world");
thanks for all
That is because the item on the left-hand side of the + is no longer a string-literal. It is now a std::string.
Regards,
Paul McKenzie
-
September 10th, 2013, 03:35 PM
#53
Re: how can i convert write() procedure to cout?
Originally Posted by Paul McKenzie
That is because the item on the left-hand side of the + is no longer a string-literal. It is now a std::string.
Regards,
Paul McKenzie
yah. but why i can't do these in my parameters list?
-
September 10th, 2013, 03:59 PM
#54
Re: [RESOLVED] how can i convert write() procedure to cout?
why i can't do these:
Code:
template <typename A, typename ...B>
void write(char *argHead, B... argTail2)
{
cout <<(string) argHead;
write(argTail2...);
}
(the error stills be when i use these function)
What happens if you change the write function definiton to
Code:
template <typename A, typename ...B>
void write(const char *argHead, B... argTail2)
{
cout << argHead;
write(argTail2...);
}
then doesn't
Code:
a.write((string("hello ") + "world").c_str());
work?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
September 10th, 2013, 04:11 PM
#55
Re: [RESOLVED] how can i convert write() procedure to cout?
If you use string like this for concatenation
Code:
a.write((string("hello ") + "world");
then doesn't your original write template function work without overloading the function template for the case of const char * or char*?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
September 10th, 2013, 04:12 PM
#56
Re: [RESOLVED] how can i convert write() procedure to cout?
Originally Posted by 2kaud
What happens if you change the write function definiton to
Code:
template <typename A, typename ...B>
void write(const char *argHead, B... argTail2)
{
cout << argHead;
write(argTail2...);
}
then doesn't
Code:
a.write((string("hello ") + "world").c_str());
work?
sorry about my english
(i understand that code works... but i mean anotherthing)
see these way that i want's:
Code:
a.write("hello " + "world")
like you see, i'm using the '+' operator for concat the char *. but the char * can't be concat directly that's why we must use casting or other functions for convert to string type. but can i change the parameter function for accept the '+' operator?
-
September 10th, 2013, 04:23 PM
#57
Re: [RESOLVED] how can i convert write() procedure to cout?
You don't need to change the paramater function. Using the original definitions for write, then
Code:
a.write((string("hello ") + "world");
should work as you are passing a type string which cout understands.
Note that if you are using 2 or more literal strings, then
"hello " + "world" can be written "hello ""world" which will be treated as "hello world" - but only for literal strings!
For your 'language', it might be easier for you to always 'convert' c-style char * strings to type string. ie
Code:
a.write((string("hello ") + string("world"));
Last edited by 2kaud; September 10th, 2013 at 04:27 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
September 10th, 2013, 04:53 PM
#58
Re: [RESOLVED] how can i convert write() procedure to cout?
Why bother with concatenation at all with your write template function? why not just do
Code:
a.write("Hello ", "world");
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
September 11th, 2013, 02:33 PM
#59
Re: [RESOLVED] how can i convert write() procedure to cout?
Originally Posted by 2kaud
Why bother with concatenation at all with your write template function? why not just do
Code:
a.write("Hello ", "world");
you have right. thanks to all. thanks for all.. thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|