C++ 将类似于{0x7fc00000}的内容分配给联合意味着什么?
我正在将一些代码从VisualStudio移植到mingw。在解决了一些问题后,我发现了以下问题。我发现了错误C++ 将类似于{0x7fc00000}的内容分配给联合意味着什么?,c++,c++03,C++,C++03,我正在将一些代码从VisualStudio移植到mingw。在解决了一些问题后,我发现了以下问题。我发现了错误 error: no matching function for call to 'DataU::DataU(const char [1])' static const DataU NAN = {0x7fc00000}; ^ 这是我正在使用的代码 static const DataU NAN = {0x7fc00000}; 这就是数据结
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))
:错误:调用“kt_flash::DataU::DataU(const char[1])”时没有匹配的函数
静态常量DataU NAN={0x7fc00000};
^
C:\Users\admin\kflash.cpp:55:20:注意:候选人是:
C:\Users\admin\kflash.cpp:11:7:注意:kt_flash::DataU::DataU()
联合大图{
^
C:\Users\admin\kflash.cpp:11:7:注意:候选者需要0个参数,提供1个参数
C:\Users\admin\kflash.cpp:11:7:注意:constexpr kt_flash::DataU::DataU(const kt_flash::DataU&)
C:\Users\admin\kflash.cpp:11:7:注意:参数1从'const char[1]'到'const kt_flash::DataU&'没有已知的转换
C:\Users\admin\kflash.cpp:11:7:注意:constexpr kt_flash::DataU::DataU(kt_flash::DataU&)
C:\Users\admin\kflash.cpp:11:7:注意:参数1从“const char[1]”到“kt_flash::DataU&&”的转换未知
C:\Users\admin\kflash.cpp:55:25:错误:应为“=”标记之前的“,”或“;”
静态常量DataU NAN={0x7fc00000};
^
在C:/mingw64/x86_64-w64-mingw32/include/d3dx9math.h:26:0中包含的文件中,
来自C:/mingw64/x86_64-w64-mingw32/include/d3dx9.h:31,
来自C:/mingw64/x86_64-w64-mingw32/include/d3dx9math.h:21,
from./ktafx.h:36,
发件人:0:
C:\Users\admin\kflash.cpp:59:25:错误:调用“(const kt_flash::DataU)(const char[1])不匹配
常量float_NAN=NAN.f;
^
进程以状态1终止(0分钟,7秒))
3个错误、3个警告(0分钟、7秒)
这是初始值设定项,不是赋值
在初始值设定项中,如:
static const DataU NAN = {0x7fc00000};
值0x7fc00000
初始化联合的第一个声明成员——在本例中为u
这是C++标准草案第8.5.1[DCL,init .AGGR ]第16段中规定的:
当使用大括号内的初始值设定项初始化联合时 大括号应仅包含第一个括号的初始值设定项条款 联合体的非静态数据成员 我希望该标准的其他版本中也有相同的措辞(C标准中也有类似的措辞)但我认为错误消息的原因是您使用了标识符
NAN
。这是在
或
中定义的宏;它扩展为表示浮点NAN(不是数字)的表达式。更改标识符可能会解决问题。(我看到[AnT的答案]()在我之前提到过这一点。)这是一个初始值设定项,不是赋值
在初始值设定项中,如:
static const DataU NAN = {0x7fc00000};
值0x7fc00000
初始化联合的第一个声明成员——在本例中为u
这是C++标准草案第8.5.1[DCL,init .AGGR ]第16段中规定的:
当使用大括号内的初始值设定项初始化联合时 大括号应仅包含第一个括号的初始值设定项条款 联合体的非静态数据成员 我希望该标准的其他版本中也有相同的措辞(C标准中也有类似的措辞)但我认为错误消息的原因是您使用了标识符
NAN
。这是在
或
中定义的宏;它扩展为表示浮点NAN(不是数字)的表达式。更改标识符可能会解决问题。(我看到[AnT的答案]()在我之前提到过这一点。)您的代码在语法和语义上一目了然。但是,NAN
是在math.h
中定义的标准C宏。这就是它触发奇怪错误的原因。您不应该在代码中使用此名称
如果将NAN
替换为其他名称,则GCC可以很好地编译代码。但是,一旦您将其命名为NAN
并包含cmath
(或math.h
),它就会触发错误。但在我的实验中,错误消息是不同的
将名称从
NAN
更改为其他名称。您的代码在语法和语义上一目了然。但是,NAN
是在math.h
中定义的标准C宏。这就是它触发奇怪错误的原因。您不应该在代码中使用此名称
如果将NAN
替换为其他名称,则GCC可以很好地编译代码。但是,一旦您将其命名为NAN
并包含cmath
(或math.h
),它就会触发错误。但在我的实验中,错误消息是不同的
将名称从
NAN
更改为其他名称。此代码。请发布给出错误的实际代码。是否有可能NAN
是在产生错误的实现中定义的宏?@AnT\include
在gotbolt的gcc中实际上是#定义NAN
,因此您正在关注一些问题清除该选项不知道为什么我仍然获得否决票。此代码。请发布给出错误的实际代码。NAN
是否有可能是在实现中定义的宏产生错误?@AnT#include