简体   繁体   English

为什么不编译此C ++代码?

[英]Why wont this C++ code compile?

I've been tasked with setting up a Debian server that will provide a service using TTK (trigger toolkit). 我的任务是设置一个Debian服务器,该服务器将使用TTK (触发工具包)提供服务。

However, the software seems to be from 1997, and unmaintained, and wont compile with GCC ( g++ ) by default (v4.7.2). 但是,该软件似乎来自1997年,并且尚未维护,并且默认情况下不会使用GCC( g++ )(v4.7.2)进行编译。

Some of the errors originated from not specifying the std namespace, and I've fixed those. 一些错误是由于未指定std名称空间而引起的,而我已修复了这些错误。

However, I'm having problems with a class that seems to be included in the software for hardware compatibility, specifically, its read functions. 但是,我遇到了一个类,该类似乎在软件中提供了问题,以实现硬件兼容性,尤其是其read功能。

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: 还有许多其他重载的read函数,它们都会吐出诸如此类的错误:

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). 似乎这段代码不了解C ++标准,我认为该标准指定istream::read应该接受一个字符数组( char * ),而不是诸如short *double *类的各种类型(我实际上是在这里阅读的,而不是在标准)。

Am I interpreting this code incorrectly? 我会错误地解释此代码吗? Has this aspect of the standard changed since 1997? 自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. s char *转换为char * C样式似乎可以消除错误,但是我不确定它是否可以解决问题。

The read function want's a pointer to the character type (ie char* in your case) as first argument. read函数需要一个指向字符类型(即您的情况下的char* )的指针作为第一个参数。 You need to use reinterpret_cast to cast the pointer correctly. 您需要使用reinterpret_cast正确投射指针。

Like 喜欢

is.read(reinterpret_cast<char*>(s), sizeof(*s))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM