-
[RESOLVED] how can i convert write() procedure to cout?
i'm building a Console class(i realy need it). and i don't understand how can i build the Write() procedure:(
heres the struture:
write(varname1[,varnamex])
how can i convert it to cout?
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Cambalinho
i'm building a Console class(i realy need it). and i don't understand how can i build the Write() procedure:(
heres the struture:
write(varname1[,varnamex])
how can i convert it to cout?
Not following what you mean. Can you clarify it more?
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
manojg
Not following what you mean. Can you clarify it more?
sample:
Code:
write( varname1, varname2, newline,...)
something like these. if i can't use the ',', then i want use the '+' or '&';)
but i don't understand how can i combine these with cout:(
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Cambalinho
sample:
Code:
write( varname1, varname2, newline,...)
something like these. if i can't use the ',', then i want use the '+' or '&';)
but i don't understand how can i combine these with cout:(
Code:
cout << varname1 << varname2 << endl;
I'm still not sure I'm understanding what you mean?
Are you wanting a function with a variable number of arguments? Have a look at
http://msdn.microsoft.com/en-us/libr...=VS.71%29.aspx
Also have a look at
http://www.cplusplus.com/reference/cstdio/vsprintf/
which describes vsprintf which creates a char array from a variable number of parameters.
It might be useful if you could provide some actual code example of what you are trying to achieve.
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
2kaud
heres the code that i have for now:
Code:
#define NewLine endl
void Write( anytype varname1,...) //any type {
int i;
for (i=0; i<NumberOfArguments; i++)
{
cout << ArgumentList[i];
}
}
the code isn't complete, but i have 2 problems:
1 - what type i can use(for use any type);
2 - how i know the number of arguments?(i think that you have in that links... i must see better;))
-
Re: how can i convert write() procedure to cout?
Code:
void Write( anytype varname1,...) //any type
{
int i;
int num;
va_list NumberOfArguments;
va_start ( NumberOfArguments, num );
for (i=0; i<num; i++)
{
cout << va_arg ( enum, anytype ); //show the variable
}
va_end (NumberOfArguments);
}
can you advice me more?
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Cambalinho
Code:
void Write( anytype varname1,...) //any type
{
int i;
int num;
va_list NumberOfArguments;
va_start ( NumberOfArguments, num );
for (i=0; i<num; i++)
{
cout << va_arg ( enum, anytype ); //show the variable
}
va_end (NumberOfArguments);
}
can you advice me more?
I think, You are looking for variable number of arguments. Go through the below post
http://stackoverflow.com/questions/1...arguments-in-c
-
Re: how can i convert write() procedure to cout?
Before reaching for variable argument lists, look at the syntax for formatted output commonly used with output streams such as std::cout. The basic idea here is to overload operator<< such that a reference to the output stream is the left hand parameter and the object to be printed is the right hand parameter. Then, these calls are chained together by returning a reference to the same output stream.
You can do something similiar for your Console class' Write function, e.g., a user of the class might chain Write calls to write objects of different types to the console in sequence. Exactly how depends on what exactly you want to do and with what syntax.
The "convert it to cout" part is too vague: are you talking about implementing your Write function to write to std::cout?
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
laserlight
Before reaching for variable argument lists, look at the syntax for formatted output commonly used with output streams such as std::cout. The basic idea here is to overload operator<< such that a reference to the output stream is the left hand parameter and the object to be printed is the right hand parameter. Then, these calls are chained together by returning a reference to the same output stream.
You can do something similiar for your Console class' Write function, e.g., a user of the class might chain Write calls to write objects of different types to the console in sequence. Exactly how depends on what exactly you want to do and with what syntax.
The "convert it to cout" part is too vague: are you talking about implementing your Write function to write to std::cout?
yes. because the prinf() is more complicated and i want be more simple;)
intead use '<<', my function uses ',' and '(',')' for open and close the function;)
but i don't understand how can i do a variable with any type:(
bagavathikumar:
now i did these code:
Code:
template<typename T, typename... Args> void Write(T t, Args... args) // recursive variadic function
{
cout << t;
Write(args...) ;
}
like you see, i'm using any type;)
(seems that site isn't comptible with VS2010:()
but i get now 4 errors:
1 - "error C2143: syntax error : missing ',' before '...'";
2 - "error C2061: syntax error : identifier 'Args'";
3 - "error C2783: 'void Console::Write(T)' : could not deduce template argument for '__formal'";
4 - "IntelliSense: no instance of function template "Console::Write" matches the argument list";
any advice?
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by Cambalinho
yes. because the prinf() is more complicated and i want be more simple
Where does prinf (printf?) come into the picture? You want to come up with something more simple than what? It almost sounds as if you think std::cout is used together with printf.
Quote:
Originally Posted by Cambalinho
intead use '<<', my function uses ',' and '(',')' for open and close the function
Looking at your example in post #3, you effectively want variable length argument list syntax. Unfortunately, if I remember correctly, the variable length argument list feature does not cater to objects of class types that do not satisfy certain requirements.
Furthermore, if you are not adding anything over the standard conventions of output streams in C++, why bother? It sounds as if you are merely planning to change the syntax, and that is a Bad Thing because it means your library users have to learn yet another thing with no value added. The only thing you have going is the vague notion of something "more simple", but sorry this:
Code:
write( varname1, varname2, newline,...)
is not "more simple" than:
Code:
std::cout << varname1 << varname2 << '\n';
If you want to take a look a project that actually does add value, read up on Boost.Format.
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
laserlight
Where does prinf (printf?) come into the picture? You want to come up with something more simple than what? It almost sounds as if you think std::cout is used together with printf.
Looking at your example in post #3, you effectively want variable length argument list syntax. Unfortunately, if I remember correctly, the variable length argument list feature does not cater to objects of class types that do not satisfy certain requirements.
Furthermore, if you are not adding anything over the standard conventions of output streams in C++, why bother? It sounds as if you are merely planning to change the syntax, and that is a Bad Thing because it means your library users have to learn yet another thing with no value added. The only thing you have going is the vague notion of something "more simple", but sorry this:
Code:
write( varname1, varname2, newline,...)
is
not "more simple" than:
Code:
std::cout << varname1 << varname2 << '\n';
If you want to take a look a project that actually does add value, read up on Boost.Format.
ok... but i need build that function;)
(i'm building the console class for my new language... that's why i need 'rebuild' the cout;))
can you advice me for correct that errors?
but can i create the 'cout'(with write keyword) in a class?
-
Re: how can i convert write() procedure to cout?
Using functions with variable number of arguments via the va_ set of macros is one of the more dusky corners of c++ inherited from c. I would strongly advise against using this mechanism if the result can be implemented some other way (eg function or operator overloading).
If you are using c++11 and really, really want functions with variable number of arguments, the answer is probably to use Variadic Templates.
http://www.open-std.org/jtc1/sc22/wg...2006/n2087.pdf
http://www.stroustrup.com/C++11FAQ.h...adic-templates
But before you get bogged down in the detail of implementing these templates, think again if you really need this functinality and that what you want to do couldn't be done some other simpler way.
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
2kaud
Using functions with variable number of arguments via the va_ set of macros is one of the more dusky corners of c++ inherited from c. I would strongly advise against using this mechanism if the result can be implemented some other way (eg function or operator overloading).
If you are using c++11 and really, really want functions with variable number of arguments, the answer is probably to use Variadic Templates.
http://www.open-std.org/jtc1/sc22/wg...2006/n2087.pdf
http://www.stroustrup.com/C++11FAQ.h...adic-templates
But before you get bogged down in the detail of implementing these templates, think again if you really need this functinality and that what you want to do couldn't be done some other simpler way.
that tutorial is limited, the 1st argument is the master and tell us, indirectly, how many arguments received:(
i need use the write() like these:
write(var1, var2, "hello world")
the order i don't care, but i don't use strings or arguments for tell me the number of arguments.
-
Re: how can i convert write() procedure to cout?
note: can i use C++11 in VS2010?
the overloading was the best thing that i found:
Code:
//Write
//1 argument
template<typename Type1>
void Write(Type1 var1) { cout << var1; }
//2 arguments
template<typename Type1, typename Type2>
void Write(Type1 var1, Type2 var2) { cout << var1 << var2; }
//3 arguments
template<typename Type1, typename Type2, typename Type3>
void Write(Type1 var1, Type2 var2, Type3 var3) { cout << var1 << var2 << var3; }
//4 arguments
template<typename Type1, typename Type2, typename Type3, typename Type4>
void Write(Type1 var1, Type2 var2, Type3 var3, Type4 var4) { cout << var1 << var2 << var3 << var4; }
//Read
//empty
void Read(){_getch();}
//1 argument
template<typename Type1>
void Read(Type1 &var1) { cin >> var1; }
//2 arguments
template<typename Type1, typename Type2>
void Read(Type1 &var1, Type2 &var2) { cin >> var1 >> var2; }
//3 arguments
template<typename Type1, typename Type2, typename Type3>
void Read(Type1 &var1, Type2 &var2, Type3 &var3) { cin >> var1 >> var2 >> var3; }
//4 arguments
template<typename Type1, typename Type2, typename Type3, typename Type4>
void Read(Type1 &var1, Type2 &var2, Type3 &var3, Type4 &var4) { cin >> var1 >> var2 >> var3 >> var4; }
thanks for all
i accept more advices;)
-
Re: how can i convert write() procedure to cout?
Try something like this. Note that variadic templates in c++11 are not yet implemented in MSVS2010 or 2012. :cry:
Code:
#include <iostream>
using namespace std;
void write()
{
cout << " ";
}
template <typename A, typename ...B>
void write(A argHead, B... argTail)
{
cout << argHead << " ";
write(argTail...);
}
int main(void)
{
write(1, 2, "qwerty", 4.45, "five", 6);
return 0;
}
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
2kaud
Try something like this. Note that variadic templates in c++11 are not yet implemented in MSVS2010 or 2012. :cry:
Code:
#include <iostream>
using namespace std;
void write()
{
cout << " ";
}
template <typename A, typename ...B>
void write(A argHead, B... argTail)
{
cout << argHead << " ";
write(argTail...);
}
int main(void)
{
write(1, 2, "qwerty", 4.45, "five", 6);
return 0;
}
sorry, but i get more than 2 errors:
1 - "error C2143: syntax error : missing ',' before '...' ";
2 - "error C2061: syntax error : identifier 'B'"
i use that library and the namespace. so what is wrong?
-
Re: how can i convert write() procedure to cout?
What compiler are you using - and does it support c++11 variadic templates? MSVS doesn't yet support these.
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
2kaud
What compiler are you using - and does it support c++11 variadic templates? MSVS doesn't yet support these.
i'm using Visual Studio 2010;)
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Cambalinho
i'm using Visual Studio 2010;)
Then thats why you get the compile errors. At present neither Visual Studio 2010 nor Visual Studio 2012 supports variadic templates. :cry:
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
2kaud
Then thats why you get the compile errors. At present neither Visual Studio 2010 nor Visual Studio 2012 supports variadic templates. :cry:
is there another way?
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Cambalinho
that tutorial is limited, the 1st argument is the master and tell us, indirectly, how many arguments received:(
It has nothing to do with the tutorial. That is how C++ works.
The receiving function has no idea where your argument list stops when you use a varying number of arguments. So you have to tell that function in some way where or when the last parameter is reached.
There are 3 general ways to specify where/when the last argument is reached.
1) A format list or some sort of string telling the function how to parse the rest of the arguments. (printf() works this way)
2) Some number stating the number of arguments that follow in the list (the examples you have work this way)
3) Some "sentinel" or ending argument, signifying the end of the argument list (functions such as execvp() work this way).
There may be other ways, but these are the first three that come to mind. But in no way can you just write a function that takes a varying number of arguments, not tell the receiving function anything, and then by magic the receiving function knows how many parameters you passed. At least not in C++ before C++ 11.
Quote:
i need use the write() like these:
write(var1, var2, "hello world")
the order i don't care, but i don't use strings or arguments for tell me the number of arguments.
Then use another language, since C++ (again, before C++11) doesn't have this functionality.
Regards,
Paul McKenzie
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Paul McKenzie
It has nothing to do with the tutorial. That is how C++ works.
The receiving function has no idea where your argument list stops when you use a varying number of arguments. So you have to tell that function in some way where or when the last parameter is reached.
There are 3 general ways to specify where/when the last argument is reached.
1) A format list or some sort of string telling the function how to parse the rest of the arguments. (printf() works this way)
2) Some number stating the number of arguments that follow in the list (the examples you have work this way)
3) Some "sentinel" or ending argument, signifying the end of the argument list (functions such as execvp() work this way).
There may be other ways, but these are the first three that come to mind. But in no way can you just write a function that takes a varying number of arguments, not tell the receiving function anything, and then by magic the receiving function knows how many parameters you passed. At least not in C++.
Then use another language, since C++ doesn't have this functionality.
Regards,
Paul McKenzie
hey the C\C++ is the most powerfull language(i think);)
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Cambalinho
hey the C\C++ is the most powerfull language(i think);)
It is powerful, but note that you can't find a function that does what you say you want it to do (at least not with C++ 98 or 03). Why do you think that is?
Regards,
Paul McKenzie
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Paul McKenzie
It is powerful, but note that you can't find a function that does what you say you want it to do (at least not with C++ 98 or 03). Why do you think that is?
Regards,
Paul McKenzie
i'm build these class for be more easy build my own programming language;)
that's why i wanted 1 funtion in that way, but seems that i must use overloading functions for the number of arguments;)
thanks for all
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Cambalinho
i'm build these class for be more easy build my own programming language;)
that's why i wanted 1 funtion in that way, but seems that i must use overloading functions for the number of arguments;)
thanks for all
The functions not only need to be overloaded, but they also need to be template functions.
Code:
template <typename arg1>
void write(arg1 a1)
{
//...
}
template <typename arg1, typename arg2>
void write(arg1 a1, arg2 a2)
{
//...
}
Regards,
Paul McKenzie
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by Cambalinho
i'm build these class for be more easy build my own programming language
Hmm... I don't see how fiddling with C++ syntax is going to make it easier for you to write a compiler/interpreter for your own programming language. Maybe you want your compiler to output C++ code that is then compiled by a C++ compiler, but between this (your preferred syntax):
Code:
write(var1, var2, "hello world")
and this (standard C++ formatted output for output stream syntax):
Code:
std::cout << var1 << var2 << "hello world"
and this (example syntax of function call chaining):
Code:
write(var1).write(var2).write("hello world")
I doubt you will face a problem with writing your compiler to output them either way, assuming you have the skill to implement for at least one of them. So, if my guess that you intend to use C++ as the output of your compiler is correct, then I suggest just using the standard formatted output.
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
laserlight
Hmm... I don't see how fiddling with C++ syntax is going to make it easier for you to write a compiler/interpreter for your own programming language. Maybe you want your compiler to output C++ code that is then compiled by a C++ compiler, but between this (your preferred syntax):
Code:
write(var1, var2, "hello world")
and this (standard C++ formatted output for output stream syntax):
Code:
std::cout << var1 << var2 << "hello world"
and this (example syntax of function call chaining):
Code:
write(var1).write(var2).write("hello world")
I doubt you will face a problem with writing your compiler to output them either way, assuming you have the skill to implement for at least one of them. So, if my guess that you intend to use C++ as the output of your compiler is correct, then I suggest just using the standard formatted output.
but:
1 - how use 'Write' word instead 'cout'?;
2 - can i add it to my class?
(i just convert my new language to C++ and then i call the C++ compiler(it's free... well the Dev C++;))
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by Cambalinho
i just convert my new language to C++ and then i call the C++ compiler
Yeah, that's what I meant by: "Maybe you want your compiler to output C++ code that is then compiled by a C++ compiler"
Quote:
Originally Posted by Cambalinho
but:
1 - how use 'Write' word instead 'cout'?;
2 - can i add it to my class?
I'm saying: don't do that since it does not gain you anything. Your compiler can just as easily output the "cout version" as it can this "Write version" that you have in mind.
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
laserlight
Yeah, that's what I meant by: "Maybe you want your compiler to output C++ code that is then compiled by a C++ compiler"
I'm saying: don't do that since it does not gain you anything. Your compiler can just as easily output the "cout version" as it can this "Write version" that you have in mind.
so instead include the Read()\Write in console class, i make them part of my own language(keywords\functions) is what you are advice me?
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by Cambalinho
so instead include the Read()\Write in console class, i make them part of my own language(keywords\functions) is what you are advice me?
It depends. I have no clue what is the syntax and semantics of this programming language that you are trying to design and implement. What I do know is that whatever the syntax, you can translate a write to standard output in that language to C++ code involving std::cout, without having to write some C++ function (template, or class) with the C++ syntax that you have in mind. In other words, concentrating on C++ seems like it is taking you off course. Rather, concentrate on the syntax and semantics of your language, and how you will be translating that into C++ without unnecessary frills.
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
laserlight
It depends. I have no clue what is the syntax and semantics of this programming language that you are trying to design and implement. What I do know is that whatever the syntax, you can translate a write to standard output in that language to C++ code involving std::cout, without having to write some C++ function (template, or class) with the C++ syntax that you have in mind. In other words, concentrating on C++ seems like it is taking you off course. Rather, concentrate on the syntax and semantics of your language, and how you will be translating that into C++ without unnecessary frills.
see these:
write("hello world" , NewLine, varname1)
to c++:
'write(' -> 'std::cout <<';
',' - > '<<';
'Newline' -> 'std::endl'
then i can delete the last ')' and add the ';'
(even for Read(varname1) is more or less the same.. except the Read() that must be '_getch()'. the 'cin' don't do the job, i think)
it's easy to translate;)
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Cambalinho
(even for Read(varname1) is more or less the same.. except the Read() that must be '_getch()'. the 'cin' don't do the job, i think)
it's easy to translate;)
There is no standard C++ function that does this job. The _getch() function may or may not be supported by the compiler.
Maybe you should go over what are the standard functions of C++ before making assumptions about how to translate your functions to C++. So far, your version of "Read" is compiler specific.
If I use a compiler that doesn't have _getch(), or uses a different name than _getch(), then your translations will not work.
Regards,
Paul McKenzie
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Paul McKenzie
There is no standard C++ function that does this job. The _getch() function may or may not be supported by the compiler.
Maybe you should go over what are the standard functions of C++ before making assumptions about how to translate your functions to C++. So far, your version of "Read" is compiler specific.
If I use a compiler that doesn't have _getch(), or uses a different name than _getch(), then your translations will not work.
Regards,
Paul McKenzie
unless i can do it with ReadConsole(), but i don't know how:(
-
Re: how can i convert write() procedure to cout?
i try these now:
ReadConsole(GetStdHandle(STD_OUTPUT_HANDLE),NULL,NULL,NULL,NULL);
but don't works:(
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Cambalinho
i try these now:
ReadConsole(GetStdHandle(STD_OUTPUT_HANDLE),NULL,NULL,NULL,NULL);
but don't works:(
STD_OUTPUT_HANDLE for input??????:rolleyes:
See
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
2kaud
STD_OUTPUT_HANDLE for input??????:rolleyes:
i never notice that lol
now i get these error:
"First-chance exception at 0x76d0e1c8 in my classs.exe: 0xC0000005: Access violation writing location 0x00000000."
why?
(after press enter, if is another key, is just showed)
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Cambalinho
i never notice that lol
now i get these error:
"First-chance exception at 0x76d0e1c8 in my classs.exe: 0xC0000005: Access violation writing location 0x00000000."
why?
(after press enter, if is another key, is just showed)
Because you haven't provided a buffer into which to read the characters! Read the documentation.
This will get 1 char from the input buffer
Code:
char ch;
DWORD read;
ReadConsole(GetStdHandle(STD_INPUT_HANDLE), &ch, 1, &read, NULL);
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
2kaud
Because you haven't provided a buffer into which to read the characters! Read the documentation.
This will get 1 char from the input buffer
Code:
char ch;
DWORD read;
ReadConsole(GetStdHandle(STD_INPUT_HANDLE), &ch, 1, &read, NULL);
thanks for all
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Cambalinho
i try these now:
ReadConsole(GetStdHandle(STD_OUTPUT_HANDLE),NULL,NULL,NULL,NULL);
but don't works:(
That is a Windows API specific function, and this forum is geared towards standard C++, not Windows API functions. There is no "ReadConsole" function in standard C++.
If you're asking about Windows API functions, then there is a WinAPI forum to ask these questions.
Regards,
Paul McKenzie
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Paul McKenzie
That is a Windows API specific function, and this forum is geared towards standard C++, not Windows API functions. There is no "ReadConsole" function in standard C++.
If you're asking about Windows API functions, then there is a WinAPI forum to ask these questions.
Regards,
Paul McKenzie
sorry about that, but the normal C++ don't have it. that's why you finished these topic with 1 API function. sorry about that
-
Re: how can i convert write() procedure to cout?
Code:
void write()
{
cout <<"";
}
template <typename A, typename ...B>
void write(A argHead, B... argTail)
{
cout << argHead;
write(argTail...);
}
template <typename A, typename ...B>
void write(char *argHead, B... argTail2)
{
string b=(string) argHead;
cout << b;
write(argTail2...);
}
is possible convert the char* type to string?
(my objective is accept the '+' for concat the strings)
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Cambalinho
is possible convert the char* type to string?
When wil you have learned to use the documentation?
http://www.cplusplus.com/reference/s...string/string/
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
VictorN
ok.. i change these:
Code:
string b=(string) argHead;
to these:
Code:
string b=string( argHead);
but when i use it:
Code:
Console b;
b.write("hello " + "world" );
i get these error:
"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+'|"
what i need is that accept that line
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Cambalinho
ok.. i change these:
Code:
string b=(string) argHead;
to these:
Code:
string b=string( argHead);
Why not just write
Code:
string b = argHead;
Quote:
Originally Posted by
Cambalinho
but when i use it:
Code:
Console b;
b.write("hello " + "world" );
i get these error:
"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+'|"
what i need is that accept that line[/QUOTE]This line has nothig to do with your previous question. Nor has it anything to do with the std::string class!
You are tryinf to add two pointers which is wrong. Is you want to concatenate thses strings - use strcat function.
-
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
VictorN
Why not just write
Code:
string b = argHead;
what i need is that accept that line
This line has nothig to do with your previous question. Nor has it anything to do with the std::string class!
You are tryinf to add two pointers which is wrong. Is you want to concatenate thses strings - use strcat function.[/QUOTE]
let me ask 1 thing:
if i can do these:
Code:
b.write((string)"hello " + "world" );
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)
?
-
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