C++编译器:'class std :: vector <std :: vector <char >>'没有名为'emplace_back'的成员。

10

当我编写C++代码时,编译器报错。以下是我的代码:

#include <iostream>     
#include <algorithm>   
#include <typeinfo>
#include <string>
#include <vector>


std::vector< std::vector<char> > p(std::vector<char> v)
{
    std::vector< std::vector<char> > result;

    std::sort(v.begin(), v.end());
    do
    {
        result.emplace_back(v);
    }
    while(std::next_permutation(v.begin(), v.end()));

    return result;
}

这是我的错误信息:

enter image description here

有什么想法是什么原因导致了这个错误吗?

我正在使用Codeblocks 12.11,Windows 7,我的编译器是GNU GCC Compiler.

感谢您的帮助 :)

更新:

如果有人遇到同样的问题,这里是解决方案(在Codeblocks 12.11中):

进入:设置 --> 编译器 --> 编译器设置 --> 选中以下复选框:

enter image description here

另外记得在代码中要有一个main函数,否则编译器将会给出如下错误:

enter image description here

解决方案是由回答我的帖子的用户提供的 :)


1
  1. GCC版本(实际上是标准库版本)是否支持C++11?
  2. 如果支持,您是否向GCC传递了适当的标志(例如-std=c++11-std=c++0x)?
- Angew is no longer proud of SO
+1 我认为它并不像 @Jeffrey 所说的那样。我该如何在 Codeblocks 中修复这个问题? - jjepsuomi
我无法勾选两个框,但通过勾选其中一个似乎代码能够正常运行。有人可以解释一下吗? - LampPost
1个回答

20

您的编译器不支持C++11。如所见,C++11中添加了std::vector<T>emplace_back成员函数。

根据您的编译器版本,您可能只需一些标志来告诉编译器打开C++11特性。在GCC和Clang上可以使用以下命令:

-std=c++11 -stdlib=libc++
否则,您可能需要升级编译器版本到更高的版本。

我不得不使用-std=gnu++14参见)。尽管我只能在rapidjson中重现这个问题。 - Albert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接