函数不是 STD 的元素
function is not an element of std
我在gcc下编译代码时遇到了一些非常奇怪的错误。它告诉我std::function
不存在。
我可以使用以下代码重新创建错误:
#include <functional>
#include <stdio.h>
void test(){ printf ("test"); }
int main() {
std::function<void()> f;
f = test;
f();
}
如果我运行 gcc(来自 cygwin):(我的错误消息是德语,所以我翻译了它。在英语 gcc 上听起来可能不同)
$ gcc test.cpp
test.cpp: in function "int main():
test.cpp:7:3: Error: "function" is not an element of "std"«
test.cpp:7:25: Error: "f" was not defined in this scope
使用 MSVC,它编译成功。请告诉我我在代码中做错了什么。
Johannes
编译为:
g++ test.cpp -std=c++0x
需要-std=c++0x
,因为您使用的是 C++11 功能,否则g++ test.cpp
就足够了。
确保您使用的是最新版本的 GCC。您可以按以下方式检查版本:
g++ --version
您需要
在C++
模式和C++11
模式下编译。因此,您需要g++
并将-std
标志设置为 c++0x
.
g++ test.cpp -std=c++0x
您也可以使用 gcc 4.7 开始的-std=c++11
。
相关文章:
- 使用std::transform将一个范围的元素添加到另一个范围中
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- 我想访问std::unique_ptr中的一个特定元素
- 如何从存储在std::映射中的std::集中删除元素
- 缓存std::数组的选定元素,并在c++中自动保持其一致性
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 使用运算符 [] 引用 std::vector 上最后一个元素时出现问题<>
- 对的排序向量 (std::vector<pair<int, int>>) 按对的第一个元素搜索并更新第二个元素值
- 为什么在 std::map 上移动无法将元素从一个映射移动到另一个映射
- 如何为 std::vector 分配内存,然后稍后为某些元素调用构造函数?
- std::矢量保存 只推送最后一个元素
- std::map:当元素不可默认构造时创建/替换元素
- 如何读/写或遍历 std::array 中的特定元素范围?
- C++如何乘以包含 std::variant 元素的向量的迭代器?正在执行迭代器类型的转换?
- 为什么 std::p air 的大小与其元素的大小之和不同?
- 我可以列表初始化 std::vector 并完美转发元素吗?
- 如何打印属于地图的一系列元素(std::p air<size_t,std::string>)(不是所有元素)
- 元素 std::vector <struct>的更有成效的段落
- 不能直接分配元素 std::array,它说没有运算符"="匹配
- 如何到达 2d std::vector 的第 N 个元素('std::vector< std::vector<T> >')?