【问题标题】:I am using c++14 or is my code, using structs, invalid?我正在使用 c++14 还是我的代码使用结构无效?
【发布时间】:2016-12-27 03:34:47
【问题描述】:

我正在重温 C++,并且一直在学习没有真正问题的教程。但是,我有一个关于 structs 的部分,它说在 c++14 中你可以同时使用 non-static initialisationunifrom 初始化。根据:-

但是,在 C++14 中,这个限制被解除,两者都可以使用。 如果两者都提供,初始化列表/统一初始化 语法优先。在上面的例子中,三角形 x 将是 用长宽2.0初始化。

我的代码是:-

struct Triangle
{
    double length = 1.23; // non-static member initialization
    double width = 2.45;
};

int Triangular()
{
Triangle x{ 2.0, 2.0 }; // uniform initialization

return 0;
}

...... 
int main() ......

但是,我无法编译它(我在 Windows 7 上使用 Code::Blocks)。如果我删除非静态初始化并使用(即删除 = 1.23= 2.45 ),那么它会编译:-

struct Triangle
{
    double length; // non-static member initialization
    double width;
};

int Triangular()
{
Triangle x{ 2.0, 2.0 }; // uniform initialization

return 0;
}

...... 
int main() ......

我的第一个想法是我没有 c++14。所以我关注了Enabling -std=c++14 flag in Code::Blocks,我似乎将编译器设置为c++14(一个例外是没有make.exe,所以make程序是mingw32-make.exe(根据Settings/Compiler/Toolchain可执行文件)。


我有:- GNU GCC 编译器

编译器设置为:-

-std=c++14 编译器标志

-std=c++98 -std=c++0x -std=c++11 取代

工具链可执行文件为:-

gcc.exe C 编译器 g++.exe C++ 编译器 g++.exe 动态库的链接器 ar.exe 静态库的链接器 GDB/CDB 调试器:默认 windres.exe 资源编译器 mingw32-make.exe 制作程序

Project Build 选项都是空白的(所以我假设使用了 Code::Block 设置。)


当我编译失败的代码(具有非静态初始化的第一个代码)时,我得到以下信息:-

构建日志

g++.exe -Wall -fexceptions -g -std=c++14  -c "D:\C++ Projects\gettingStarted\main.cpp" -o obj\Debug\main.o
D:\C++ Projects\gettingStarted\main.cpp: In function 'int Triangular()':
D:\C++ Projects\gettingStarted\main.cpp:24:22: error: no matching function for call to 'Triangle::Triangle(<brace-enclosed initializer list>)'
 Triangle x{ 2.0, 2.0 }; // uniform initialization
                      ^
D:\C++ Projects\gettingStarted\main.cpp:24:22: note: candidates are:
D:\C++ Projects\gettingStarted\main.cpp:16:8: note: constexpr Triangle::Triangle()
 struct Triangle
        ^
D:\C++ Projects\gettingStarted\main.cpp:16:8: note:   candidate expects 0 arguments, 2 provided
D:\C++ Projects\gettingStarted\main.cpp:16:8: note: constexpr Triangle::Triangle(const Triangle&)
D:\C++ Projects\gettingStarted\main.cpp:16:8: note:   candidate expects 1 argument, 2 provided
D:\C++ Projects\gettingStarted\main.cpp:16:8: note: constexpr Triangle::Triangle(Triangle&&)
D:\C++ Projects\gettingStarted\main.cpp:16:8: note:   candidate expects 1 argument, 2 provided
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))

使用第二个代码(已删除非静态初始化)我只是收到关于 x 未被使用的警告(我可以管理它):-

构建日志

-------------- Build: Debug in gettingStarted (compiler: GNU GCC Compiler)---------------

g++.exe -Wall -fexceptions -g -std=c++14  -c "D:\C++ Projects\gettingStarted\main.cpp" -o obj\Debug\main.o
D:\C++ Projects\gettingStarted\main.cpp: In function 'int Triangular()':
D:\C++ Projects\gettingStarted\main.cpp:24:10: warning: unused variable 'x' [-Wunused-variable]
 Triangle x{ 2.0, 2.0 }; // uniform initialization
          ^
g++.exe  -o bin\Debug\gettingStarted.exe obj\Debug\main.o obj\Debug\myotherfile.o   
Output file is bin\Debug\gettingStarted.exe with size 1.07 MB
Process terminated with status 0 (0 minute(s), 1 second(s))
0 error(s), 1 warning(s) (0 minute(s), 1 second(s))

所以我相信我要么使用了无效代码,要么我没有使用 c++14 编译,而是使用 c++11。问题是其中之一,还是其他问题?

【问题讨论】:

  • 您的 GCC 编译器版本不支持 C++14...只是猜测,也是因为我不知道 C++14 或任何其他版本的 C++ 中的此功能.
  • 我正在从MinGW Distro - nuwen.net 下载可能是更高版本的 MinGW。也许这会奏效。不是需要这个功能,而是我可以从解决问题中学习。
  • 我相信编译器是问题所在。但是,我现在面临一个问题,我认为是我的 Windows 7 版本/具有 64 位处理器。即我得到c:/mingw/mingw/bin/../lib/gcc/x86_64-w64-mingw32/6.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: i386 architecture of input file ``obj\Debug\myotherfile.o' is incompatible with i386:x86-64 output collect2.exe: error: ld returned 1 exit status 我想我会恢复到提供的MinGW,而不是按照c ++ 11使用两种类型的初始化。也许当我更熟悉 C++ 时。我会考虑解决这个问题。
  • @MikeT 您可以尝试从here 下载 64 位 MinGW 工具链。我从来没有遇到过这些问题。
  • 好的,根据之前的评论,问题似乎与目标文件有关。需要清洁。

标签: c++ c++11 compiler-errors c++14


【解决方案1】:
Triangle x{ 2.0, 2.0 };

上面的行试图执行Triangle 的聚合初始化。但是,在 C++11 中,非静态数据成员初始化器或 默认成员初始化器(现在称为)的存在阻止了 Triangle 成为聚合,并且初始化将失败。

这条规则是modified in C++14,现在允许聚合有默认的成员初始化器。看来你的编译器还不支持。您的示例在符合 C++14 的编译器上执行 compile

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 1970-01-01
    相关资源
    最近更新 更多