Hi, folks:
i have written a program that uses an iostream member type iostate, i worte some code like those at the beginning of source file, but it seems wrong:
does anybody know that? The iostate is a Standard library member type defined in ios_base header, but i don't know how to include it in my program, i would appreciate it very much if you tell me... well , all i want is to output the size of the iostate, i wrote some thing like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<iostream>
#include<istream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int num;
cout << "Please enter a integer: " << endl;
cin >> num;
if(cin.good() )cout << "The integer is: " << num <<endl;
else {
istream::iostate state = cin.rdstate();
cout << state << endl;
}
return 0;
}
All i want is to output the state of cin! But when i compile, the compiler tells me " istream has not been declared! iostate was not declared in this scope! " What's wrong? anyone helps! Thanks!
Thanks, guestgulkan, it works!
but there still is one problem remains:
why do i have to prefix iostate by istream:: in the expression statement when i have already used using declaration at the beginning of my source file? That is, why doesn't the following code work?
1 2
using std::istream;
iostate state = cin.rdstate();
i doubt this because after i declaredusing std::cin, i can use cin in expression statement directly! Can you tell me how i can use iostate directly? Thanks!
I'd have to check through the varios headers, but if I remember correctly - iostate is a enum defined in the ios_base class which is inherited by istream and ostream
So iostate is not directly defined in the std namespace - that is why it is: std::istream::iostate or std::ostream::iostate or std::ios_base::iostate,
why do i have to prefix iostate by istream:: in the expression statement when i have already used using declaration at the beginning of my source file? That is, why doesn't the following code work?
That is because istream is a class name and not a namespace. Actually iostate is a member of ios_base, but you can access it through istream since it inherits it from ios, and which ios inherits from ios_base. http://cplusplus.com/reference/iostream/
Can you tell me how i can use iostate directly?
You can define a new type.
1 2 3 4
usingnamespace std;
typedef ios_base::iostate iostate;
iostate state = cin.rdstate();