Re: is possible create a any type variable(1 variable that accept any type of values)
Quote:
Originally Posted by
2kaud
Now that you have a c++11 compiler, you might want to revisit where all this started - How can I convert write procedure to cout
http://forums.codeguru.com/showthrea...cedure-to-cout
and see my post #15 with the solution using variadic templates. As gcc 4.8.1 supports variadic templates, this code should compile and give you the write function you originally wanted without using the variant class - which I suspect is going to give more trouble than it's worth:eek:.
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;
}
thanks.
now i'm doing the same for read(): but i get errors:
Code:
void read()
{
cin >> " ";
}
template <typename A, typename ...B>
void read(A argHead, B... argTail)
{
cin >> argHead >> "";
read(argTail...);
}
don't forget theres these type of read too:
Code:
void read()
{
DWORD oldMode;
GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &oldMode);
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT);
FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
char buffer[1];
DWORD read;
ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &read, NULL);
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), oldMode);
CloseHandle(GetStdHandle(STD_INPUT_HANDLE));
}
thanks you for all my friend.. thank you both
Re: is possible create a any type variable(1 variable that accept any type of values)
Code:
void read()
{
cin >> " ";
}
No! You cannot do a stream extraction to a literal string!:rolleyes: Just comment out the cin line.
Code:
cin >> argHead >> "";
Again, you cannot do a stream extraction to a literal string.
Just do
Also the parameters for read should be passed by reference not by value as you want to pass back the value read.
Re: is possible create a any type variable(1 variable that accept any type of values)
Quote:
Originally Posted by
2kaud
Code:
void read()
{
cin >> " ";
}
No! You cannot do a stream extraction to a literal string!:rolleyes: Just comment out the cin line.
Code:
cin >> argHead >> "";
Again, you cannot do a stream extraction to a literal string.
Just do
Also the parameters for read should be passed by reference not by value as you want to pass back the value read.
the variable isn't changed:(
Code:
void read()
{
//cin;
}
template <typename A, typename ...B>
void read(A argHead, B... argTail)
{
cin >> argHead ;
read(&argTail...);
}
Re: is possible create a any type variable(1 variable that accept any type of values)
now works fine:
Code:
void read()
{
//cin;
}
template <typename A, typename ...B>
void read(A &argHead, B... argTail)
{
cin >> argHead ;
read(argTail...);
}
but if i use these tooo:
Code:
void read(void)
{
DWORD oldMode;
GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &oldMode);
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT);
FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
char buffer[1];
DWORD read;
ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &read, NULL);
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), oldMode);
CloseHandle(GetStdHandle(STD_INPUT_HANDLE));
}
i get an error:
"|184|error: 'void Console::read()' cannot be overloaded|"
can you advice me?
Re: is possible create a any type variable(1 variable that accept any type of values)
Because as part of your variadic template read, you have the function
so you cannot have another function with the definition void read(void) - which is the same as void read()!
However, for your ReadConsole read function, it takes no arguments and returns void - so what happens to what it reads and what's its purpose? I would suggest changing its function name to something like readcon(..). Also, why are you closing the handle for standard input?
Re: is possible create a any type variable(1 variable that accept any type of values)
Quote:
Originally Posted by
2kaud
Because as part of your variadic template read, you have the function
so you cannot have another function with the definition void read(void) - which is the same as void read()!
However, for your ReadConsole read function, it takes no arguments and returns void - so what happens to what it reads and what's its purpose? I would suggest changing its function name to something like readcon(..). Also, why are you closing the handle for standard input?
that function is only for wait the user click in enter for continue\exit;)
Re: is possible create a any type variable(1 variable that accept any type of values)
yes i can do it hehehehe;)
seems that both functions are called;)
but i simply create a boolean variable and now seems to work....
see the code:
Code:
class Console{
private:
char pszOldWindowTitle[MY_BUFSIZE];
HWND ConsoleHandle;
HDC ConsoleDC;
bool blnVisible;
bool ReadEnter=false; //these boolean variable is for distinguish the functions used;)
...........................................................................
//see now the functions:
void read() {
if (ReadEnter==false)
{
DWORD oldMode;
GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &oldMode);
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT);
FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
char buffer[1];
DWORD read;
ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &read, NULL);
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), oldMode);
CloseHandle(GetStdHandle(STD_INPUT_HANDLE));
}
}
template <typename A, typename ...B>
void read(A &argHead, B... argTail)
{
ReadEnter=true;
cin >> argHead ;
read(argTail...);
ReadEnter=false;
}
and seems to work normaly.... sometimes my brain works more hehehehe;)
but tell me what you think;)
heres how use them:
Code:
Console a; int c=10;
a.read(c) ;
a.write(c);
a.read();
let me ask you 1 last thing about header files. how can i test if the header file was called?
(for don't give about the function be double declared(or something like it))
thanks for all my friend.. thanks to both
(i'm trying rate you, both, but isn't easy:()
Re: is possible create a any type variable(1 variable that accept any type of values)
Quote:
let me ask you 1 last thing about header files. how can i test if the header file was called?
??? :confused:You don't call a header file. You include a header file to provide the definitions for class/functions etc. If your code uses a class or function defined in a header and the header hasn't been included then the compiler will soon complain.
If you mean how do you prevent the header file from being included more than once, then there are two ways
The simplest is to have the statement
as the first line of the header file. However, not all compilers recognise this. The 'standard' way is to use a 'guard define' like this
Code:
#ifndef myheadguard1
#define myheadguard1
//insert contents of header here
#endif
where myheadguard1 is unique for each header.
You might also want to consider using a namespace for your class.
Re: is possible create a any type variable(1 variable that accept any type of values)
Quote:
Originally Posted by
2kaud
??? :confused:You don't call a header file. You include a header file to provide the definitions for class/functions etc. If your code uses a class or function defined in a header and the header hasn't been included then the compiler will soon complain.
If you mean how do you prevent the header file from being included more than once, then there are two ways
The simplest is to have the statement
as the first line of the header file. However, not all compilers recognise this. The 'standard' way is to use a 'guard define' like this
Code:
#ifndef myheadguard1
#define myheadguard1
//insert contents of header here
#endif
where myheadguard1 is unique for each header.
You might also want to consider using a namespace for your class.
why use a namespace, if is only use 1 class?
thanks for all
Re: is possible create a any type variable(1 variable that accept any type of values)
Quote:
Originally Posted by
Cambalinho
why use a namespace, if is only use 1 class?
thanks for all
It is 1 class now, but what about tomorrow, next month, next year? You don't know the future.
What if you write new code, and your name clashes with an existing name from another class that may not be yours? For example, what if you created a class called "list", or "string" or "map", etc. and you now #include a standard header that just happens to have classes with the same name?
Regards,
Paul McKenzie
Re: is possible create a any type variable(1 variable that accept any type of values)
Quote:
seems that both functions are called
but i simply create a boolean variable and now seems to work....
and seems to work normaly.... sometimes my brain works more
but tell me what you think
I would advise caution about mixing c++ io streaming (using cin, cout etc) with the low-level console functions (ReadConsole etc) - especially on input as 'strange' results can happen if the streaming io buffers are not empty before using the ReadConsole/WriteConsole group of functions.
Re: is possible create a any type variable(1 variable that accept any type of values)
Quote:
Originally Posted by
2kaud
I would advise caution about mixing c++ io streaming (using cin, cout etc) with the low-level console functions (ReadConsole etc) - especially on input as 'strange' results can happen if the streaming io buffers are not empty before using the ReadConsole/WriteConsole group of functions.
so what is the best way for clean the streaming io buffers?
(maybe that's why the getch(), sometimes, is ignored;))