Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ g++;错误:‘;占位符’;不是使用命名空间std::占位符的命名空间名称;_C++_Functional Programming_Stdbind - Fatal编程技术网

C++ g++;错误:‘;占位符’;不是使用命名空间std::占位符的命名空间名称;

C++ g++;错误:‘;占位符’;不是使用命名空间std::占位符的命名空间名称;,c++,functional-programming,stdbind,C++,Functional Programming,Stdbind,我正在努力理解std::bind。我已经编写了以下程序 #include <memory> #inc

我正在努力理解
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));
}

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


通常,当您试图使用C++11特性而不使用正确的开关时,g++会在错误消息中提到;但不幸的是,这似乎不是其中一次。

@M.M-这确实有效,谢谢。
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)
     ^