![](/img/trans.png)
[英]What does “request for member '*******' in something not a structure or union” mean?
[英]What does assigning something like {0x7fc00000} to a union mean?
我正在将一些代码从Visual Studio移植到mingw。 解决了一些问题后,我介绍了以下问题。 我收到错误
error: no matching function for call to 'DataU::DataU(const char [1])'
static const DataU NAN = {0x7fc00000};
^
这是我正在使用的代码
static const DataU NAN = {0x7fc00000};
这是数据结构
union DataU {
uint32_t u;
float f;
};
我在工会方面没有太多经验,但是我从这里获得了基础知识
我仍然对为什么我在GCC中的这一陈述上出现错误感到困惑。
static const DataU NAN = {0x7fc00000};
据我了解,应该调用DataU的副本构造函数。 但是,该联合没有自定义副本构造函数。 这是C ++ 03,我不明白为什么在这里使用{}
。 关于如何解决此问题的任何建议将不胜感激。
更新:我真的不确定从哪里得到此错误。 但是我希望这个输出可以解决这个问题
: error: no matching function for call to 'kt_flash::DataU::DataU(const char [1])'
static const DataU NAN = {0x7fc00000};
^
C:\Users\admin\kflash.cpp:55:20: note: candidates are:
C:\Users\admin\kflash.cpp:11:7: note: kt_flash::DataU::DataU()
union DataU {
^
C:\Users\admin\kflash.cpp:11:7: note: candidate expects 0 arguments, 1 provided
C:\Users\admin\kflash.cpp:11:7: note: constexpr kt_flash::DataU::DataU(const kt_flash::DataU&)
C:\Users\admin\kflash.cpp:11:7: note: no known conversion for argument 1 from 'const char [1]' to 'const kt_flash::DataU&'
C:\Users\admin\kflash.cpp:11:7: note: constexpr kt_flash::DataU::DataU(kt_flash::DataU&&)
C:\Users\admin\kflash.cpp:11:7: note: no known conversion for argument 1 from 'const char [1]' to 'kt_flash::DataU&&'
C:\Users\admin\kflash.cpp:55:25: error: expected ',' or ';' before '=' token
static const DataU NAN = {0x7fc00000};
^
In file included from C:/mingw64/x86_64-w64-mingw32/include/d3dx9math.h:26:0,
from C:/mingw64/x86_64-w64-mingw32/include/d3dx9.h:31,
from C:/mingw64/x86_64-w64-mingw32/include/d3dx9math.h:21,
from ./ktafx.h:36,
from <command-line>:0:
C:\Users\admin\kflash.cpp:59:25: error: no match for call to '(const kt_flash::DataU) (const char [1])'
const float FLOAT_NAN = NAN.f;
^
Process terminated with status 1 (0 minute(s), 7 second(s))
3 error(s), 3 warning(s) (0 minute(s), 7 second(s))
那是一个初始化器,而不是一个赋值。
在像这样的初始化程序中:
static const DataU NAN = {0x7fc00000};
值0x7fc00000
初始化联合的第一个声明的成员-在这种情况下为u
。
这在C ++标准的N4296草案的8.5.1 [dcl.init.aggr]第16节中指定:
当用大括号括起来的初始化程序初始化联合时,大括号应仅包含该联合的第一个非静态数据成员的初始化子句 。
我希望在其他版本的标准中使用相同的措辞(在C标准中也是如此)。
但是我认为错误消息的原因是您使用标识符NAN
。 那是在<cmath>
或<math.h>
定义的宏; 它扩展为一个表示浮点NaN(非数字)的表达式。 更改标识符可能会解决该问题。 (我发现[AnT的答案'( https://stackoverflow.com/a/29357570/827263 )在我这样做之前就提到了这一点。)
乍一看,您的代码在语法和语义上都很好。 但是, NAN
是math.h
定义的标准C宏。 这就是它触发奇怪错误的原因。 您不应在代码中使用此名称。
如果将NAN
替换为其他名称,则GCC可以很好地编译代码。 但是,一旦您将其命名为NAN
并包含cmath
(或math.h
),就会触发错误。 在我的实验中,错误消息有所不同。
将名称从NAN
更改为其他名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.