gpt4 book ai didi

c++ - g++ 错误: ‘placeholders’ 不是使用命名空间 std::placeholders 的命名空间名称;

转载 作者:行者123 更新时间:2023-11-30 05:31:46 24 4
gpt4 key购买 nike

我正在尝试理解 std::bind。我编写了以下程序。

#include <memory>                                                                                                                                                                                                  
#include <functional>
#include <iostream>
#include <algorithm>

using namespace std::placeholders;

int add(int first,int second)
{
return first + second;

}

bool divisible(int num, int den)
{
if (num %den == 0)
return true;
return false;
}

int approach_1()
{
int arr[10] = {1,20,13,4,5,6,10,28,19,15};
int count = 0;

for (int i = 0; i < sizeof(arr)/sizeof(int); i++)
{
if(divisible(arr[i],5))
count++;
}
}

int approach_2()
{
int arr[10] = {1,20,13,4,5,6,10,28,19,15};
return count_if(arr,arr + sizeof(arr)/sizeof(int), std::bind(divisible,_1,5));
}

出于某种原因,我的 g++ 编译器无法识别 std::placeholders 命名空间。

这些是我得到的错误。

std_bind.cpp:6:22: error: ‘placeholders’ is not a namespace-name
using namespace std::placeholders;
^
std_bind.cpp:6:34: error: expected namespace-name before ‘;’ token
using namespace std::placeholders;
^
std_bind.cpp: In function ‘int approach_2()’:
std_bind.cpp:36:54: error: ‘bind’ is not a member of ‘std’
return count_if(arr,arr + sizeof(arr)/sizeof(int), std::bind(divisible,_1,5));
^
std_bind.cpp:36:74: error: ‘_1’ was not declared in this scope
return count_if(arr,arr + sizeof(arr)/sizeof(int), std::bind(divisible,_1,5));
^
std_bind.cpp:36:79: error: ‘count_if’ was not declared in this scope
return count_if(arr,arr + sizeof(arr)/sizeof(int), std::bind(divisible,_1,5));
^
std_bind.cpp:36:79: note: suggested alternative:
In file included from /usr/include/c++/4.9/algorithm:62:0,
from std_bind.cpp:4:
/usr/include/c++/4.9/bits/stl_algo.h:3970:5: note: ‘std::count_if’
count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
^

最佳答案

std::placeholders 是在 C++11 中添加的,因此对于 g++,您必须使用启用 C++11 或更高版本的编译器开关;例如 -std=c++11-std=c++14

通常,当您尝试在未使用正确开关的情况下使用 C++11 功能时,g++ 会在错误消息中提及;但不幸的是,这似乎不是那个时代之一。

关于c++ - g++ 错误: ‘placeholders’ 不是使用命名空间 std::placeholders 的命名空间名称;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35418113/

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