2

I've been tasked with setting up a Debian server that will provide a service using TTK (trigger toolkit).

However, the software seems to be from 1997, and unmaintained, and wont compile with GCC (g++) by default (v4.7.2).

Some of the errors originated from not specifying the std namespace, and I've fixed those.

However, I'm having problems with a class that seems to be included in the software for hardware compatibility, specifically, its read functions.

The class, from what I can understand, is supposed to abstract endianness of the hardware (or something of a similar nature):

public:
    Architecture()
    {
      short y = 256;
      short *x=&y;
      alpha_byte_ordering = ( *( (char*) (x) + 1) ) == 1 ? 1 : 0;
      if (alpha_byte_ordering)
        fprintf(stderr, "Architecture: detected alpha byte ordering.\n");
      else fprintf(stderr, "Architecture: detected non-alpha byte ordering.\n");
    }

    int read(istream &is, short *s)
    {
      if (!is.read(s, sizeof(short))) return 0;
      if (!alpha_byte_ordering) reverse_byte_order(s);
      return 1;
    }

There are many other overloaded read functions, and they all spit out errors such as this one:

Architecture.H: In member function ‘int Architecture::read(std::istream&, short int*)’:
Architecture.H:31:34: error: no matching function for call to ‘std::basic_istream<char>::read(short int*&, long unsigned int)’

It seems as if this code was ignorant of the C++ standard which I think specifies that istream::read should accept a character array (char *), not varying types such as short * and double * (I actually read that here, not in the standard).

Am I interpreting this code incorrectly? Has this aspect of the standard changed since 1997? Or was this code simply non-functional to begin with (I highly doubt this since there are papers written about it)?

Lastly, how can I fix this? Casting s to char * C-style seems to remove the errors but I'm not sure whether it fixes the problem or not.

corazza
  • 31,222
  • 37
  • 115
  • 186

1 Answers1

0

The read function want's a pointer to the character type (i.e. char* in your case) as first argument. You need to use reinterpret_cast to cast the pointer correctly.

Like

is.read(reinterpret_cast<char*>(s), sizeof(*s))
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • This seems to do the trick. However I thought `reinterpret_cast` was only used for polymorphic types, and that C-style casts were enough for fundamental types and pointers... – corazza Aug 06 '13 at 11:29
  • @Yannbane `reinterpret_cast` is the C++ way of doing C-style casts, while `static_cast` and `dynamic_cast` is for polymorphic types. – Some programmer dude Aug 06 '13 at 11:30
  • @Yannbane see [this question](http://stackoverflow.com/questions/8427107/c-style-cast-vs-reinterpret-cast) for a detailed explanation – nikolas Aug 06 '13 at 11:31