invalid conversion

Dear c/g++ experts:
invalid conversion from ‘int’ to ‘std::ios_base::iostate’
when I tried to compile a piece of code, from book which claimed good on vc++7.1
on window xp.
but I got above compile error on my g++ 4.5.2

ios_base::iostate state = 0;

plz help, thanks a lot in advance, Eric
That's because it has a typesafe hack to prevent assignment from integers...
Use ios_base::iostate state = ios_base::iostate();
hhh
thanks your reply, it can compile now,
but its result did not run good
-------
root@eric-laptop:/home/eric/cppcookbook/ch13# ./a.out
terminate called after throwing an instance of 'std::runtime_error'
what(): locale::facet::_S_create_c_locale name not valid
Aborted
root@eric-laptop:/home/eric/cppcookbook/ch13#
-------------
original program is from 13-4.cpp of book (c++ cookbook, page 472, 473)
http://examples.oreilly.com/9780596007614/
you can download and read and test/run by yourself
the book authors expect the run result should be
3/28/2005
28.03.2005
or today's date
hope you can commit to port this vc++ code to gcc/g++, thanks a lot in advance, Eric
That's not a problem with the code, but a problem with Windows and MinGW. When the code runs it cannot find the "English" locale. (Nor would it find the "German" locale.)

On Windows, using G++, locales just don't work that well. There are options to fix that (sort of), but I don't know that much about it (having had little success and not caring enough to spend a lot of time on it yet...)

The real problem is that the C++ standard states that locale support is implementation defined, so there is no standard way to use a non-C locale. Linux systems seem to have applied themselves to a standard (things like "en_US" work), but that's unfortunately all there is to it.

I tested the code thus:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <ctime>
#include <locale>
#include <sstream>
#include <iterator>

using namespace std;

void translateDate(istream& in, ostream& out) {

   // Create a date reader
   const time_get<char>& dateReader =
     use_facet<time_get<char> >(in.getloc( ));

   // Create a state object, which the facets will use to tell
   // us if there was a problem.
   ios_base::iostate state = ios_base::iostate();

   // End marker
   istreambuf_iterator<char> end;

   tm t; // Time struct (from <ctime>)

   // Now that all that's out of the way, read in the date from
   // the input stream and put it in a time struct.
   dateReader.get_date(in, end, in, state, &t);

   // Now the date is in a tm struct. Print it to the out stream
   // using its locale. Make sure you only print out what you
   // know is valid in t.
   if (state == 0 || state == ios_base::eofbit) {
      // The read succeeded.
      const time_put<char>& dateWriter =
        use_facet<time_put<char> >(out.getloc( ));

      char fmt[] = "%x";

      if (dateWriter.put(out, out, out.fill( ),
                         &t, &fmt[0], &fmt[2]).failed( ))
         cerr << "Unable to write to output stream.\n";
   } else {
      cerr << "Unable to read cin!\n";
   }
}

int main( ) {

//   cin.imbue(locale("english"));
//   cout.imbue(locale("german"));
   translateDate(cin, cout);
}

Sorry.
Topic archived. No new replies allowed.