-
August 25th, 2013, 03:50 PM
#31
Re: is possible create a any type variable(1 variable that accept any type of values)
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 .
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
-
August 25th, 2013, 03:59 PM
#32
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! 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.
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)
-
August 25th, 2013, 04:25 PM
#33
Re: is possible create a any type variable(1 variable that accept any type of values)
Originally Posted by 2kaud
Code:
void read()
{
cin >> " ";
}
No! You cannot do a stream extraction to a literal string! 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...);
}
-
August 25th, 2013, 04:31 PM
#34
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?
-
August 25th, 2013, 04:57 PM
#35
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?
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)
-
August 25th, 2013, 05:11 PM
#36
Re: is possible create a any type variable(1 variable that accept any type of values)
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
-
August 25th, 2013, 05:22 PM
#37
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)
-
August 25th, 2013, 05:35 PM
#38
Re: is possible create a any type variable(1 variable that accept any type of values)
let me ask you 1 last thing about header files. how can i test if the header file was called?
??? 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.
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)
-
August 25th, 2013, 05:39 PM
#39
Re: is possible create a any type variable(1 variable that accept any type of values)
Originally Posted by 2kaud
??? 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
-
August 26th, 2013, 05:36 AM
#40
Re: is possible create a any type variable(1 variable that accept any type of values)
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
-
August 26th, 2013, 06:38 AM
#41
Re: is possible create a any type variable(1 variable that accept any type of values)
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.
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)
-
August 26th, 2013, 06:49 AM
#42
Re: is possible create a any type variable(1 variable that accept any type of values)
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)
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
|