Re: how can i convert write() procedure to cout?
Quote:
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
Re: how can i convert write() procedure to cout?
Quote:
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))
Re: how can i convert write() procedure to cout?
Quote:
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
Re: how can i convert write() procedure to cout?
Quote:
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
Re: how can i convert write() procedure to cout?
Quote:
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
Re: how can i convert write() procedure to cout?
Quote:
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
Re: how can i convert write() procedure to cout?
Quote:
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
Re: how can i convert write() procedure to cout?
Quote:
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?
Re: [RESOLVED] how can i convert write() procedure to cout?
Quote:
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?
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*?
Re: [RESOLVED] how can i convert write() procedure to cout?
Quote:
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?
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"));
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");
Re: [RESOLVED] how can i convert write() procedure to cout?
Quote:
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