c++ - Why is trying to store a pointer to function ambiguous -
here code:
#include <functional> #include <iostream> #include<vector> using namespace std; // vector iterator template <class t> class vit { private: //vector<t>::iterator it; vector<t> m_v; function<bool (t, t)> m_fptr; int len, pos; public: vit(vector<t> &v) { this->m_v = v; len = v.size(); pos = 0;}; // it= v.begin(); }; bool next(t &i) { //if(it == m_v.end()) return false; if(pos==len) return false; //i = *it; = m_v[pos]; //if(idle) { idle = false ; return true; } //it++; pos++; return true;}; //bool idle = true; void set_same(function<bool (t,t)> fptr) { m_fptr = fptr ;}; //void set_same(function<bool(int, int)> fun) { return ; } bool grp_begin() { return pos == 0 || ! m_fptr(m_v[pos], m_v[pos-1]); }; bool grp_end() { return pos == len || ! m_fptr(m_v[pos], m_v[pos+1]); }; }; bool is_same(int a, int b) { return == b; } main() { vector<int> v ={ 1, 1, 2, 2, 2, 3, 1, 1, 1 }; int total; for(auto = v.begin(); != v.end(); it++) { if(it == v.begin() || *it != *(it-1)) { total = 0; } total += *it; if(it+1 == v.end() || *it != *(it+1)) { cout << total << endl; } } cout << "let's gry group" <<endl; vit<int> g(v); int i; while(g.next(i)) { cout << << endl; } cout << "now let's fancy" << endl; vit<int> a_vit(v); //auto is_same = [](int a, int b) { return == b; }; a_vit.set_same(is_same); //int total; while(a_vit.next(i)) { if(a_vit.grp_begin()) total = 0; total += i; if(a_vit.grp_end()) cout << total << endl ; } }
when compile g++ -std=c++11 iter.cc -o iter, result:
iter.cc: in function 'int main()': iter.cc:63:17: error: reference 'is_same' ambiguous a_vit.set_same(is_same); ^ iter.cc:37:6: note: candidates are: bool is_same(int, int) bool is_same(int a, int b) { return == b; } ^ in file included /usr/include/c++/5.3.0/bits/move.h:57:0, /usr/include/c++/5.3.0/bits/stl_pair.h:59, /usr/include/c++/5.3.0/utility:70, /usr/include/c++/5.3.0/tuple:38, /usr/include/c++/5.3.0/functional:55, iter.cc:1: /usr/include/c++/5.3.0/type_traits:958:12: note: template<class, class> struct std::is_same struct is_same; ^
by way of explanation, have created class called 'vit'. 2 things: iterate on vector, , determine if new group has been reached.
the class function 'set_same' supposed store function provided calling class determine if 2 adjacent elements of vector in same group. however, can't seem store function in class future use grp_begin() , grp_end() on account of ostensible ambiguity of is_same.
what gives?
there is_same
function defined , there struct is_same
defined by c++ standard library. since using namespace std
, compiler doesn't know is_same
meant use.
Comments
Post a Comment