gpt4 book ai didi

c++ - 如何在C++中编译模板类

转载 作者:太空宇宙 更新时间:2023-11-04 13:42:29 25 4
gpt4 key购买 nike

我在程序中使用的每个模板类都出现错误。即使当我从网站上复制一个解决方案并尝试编译时,它也会出现错误,例如 ,

set_prac.cc: In function ‘int main()’:
set_prac.cc:10:13: error: ‘it’ does not name a type
for (auto it = std::begin(foo); it!=std::end(foo); ++it)
^
set_prac.cc:10:35: error: expected ‘;’ before ‘it’
for (auto it = std::begin(foo); it!=std::end(foo); ++it)
^
set_prac.cc:10:35: error: ‘it’ was not declared in this scope
set_prac.cc:10:39: error: ‘end’ is not a member of ‘std’
for (auto it = std::begin(foo); it!=std::end(foo); ++it)

.........................................

程序是, http://www.cplusplus.com/reference/iterator/begin/

并且它在他们的网站上运行良好。编译具有模板的 C++ 代码是否需要任何额外的功能?

我正在编译它,

g++ 示例.cc

最佳答案

正如其他人在评论中指出的那样,如果您使用的是 C++11 功能,则需要包含 -std=c++11 因为 gcc 默认采用 c++03。根据您使用的 gcc 版本,您可能需要改用 -std=c++0x:

g++ -pedantic -std=c++0x example.cc -o example

关于c++ - 如何在C++中编译模板类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27261955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com