c++ - 为什么试图存储指向函数的指针不明确

标签 c++ function c++11

这是我的代码:

    #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;
                    i = 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 a == b; }

    main()
    {
            vector<int>  v ={ 1, 1, 2, 2, 2, 3, 1, 1, 1 };
            int total;
            for(auto it = v.begin(); it != 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 a group" <<endl;
            vit<int> g(v);
            int i;
            while(g.next(i)) { cout << i << endl; }

            cout << "now let's get really fancy" << endl;
            vit<int> a_vit(v);
            //auto is_same = [](int a, int b) { return a == 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 ;
            }
    }        

当我用 g++ -std=c++11 iter.cc -o iter 编译它时,我得到了结果:

    iter.cc: In function 'int main()':
    iter.cc:63:17: error: reference to 'is_same' is 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 a == b; }
          ^
    In file included from /usr/include/c++/5.3.0/bits/move.h:57:0,
                     from /usr/include/c++/5.3.0/bits/stl_pair.h:59,
                     from /usr/include/c++/5.3.0/utility:70,
                     from /usr/include/c++/5.3.0/tuple:38,
                     from /usr/include/c++/5.3.0/functional:55,
                     from iter.cc:1:
    /usr/include/c++/5.3.0/type_traits:958:12: note:                         template<class, class> struct std::is_same
         struct is_same;
                ^

作为解释,我创建了一个名为“vit”的类。它做了两件事:遍历一个 vector ,并确定是否到达了一个新的组。

类函数“set_same”应该存储调用类提供的函数,以确定 vector 的两个相邻元素是否在同一组中。但是,由于 is_same 表面上的歧义,我似乎无法将函数存储在类中以供 grp_begin() 和 grp_end() 将来使用。

什么给了?

最佳答案

有一个 is_same 函数由您定义,并且有一个结构 is_same 定义 by the C++ Standard Library .由于您正在使用命名空间 std,您的编译器不知道您打算使用哪个 is_same

关于c++ - 为什么试图存储指向函数的指针不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35674408/

相关文章:

c++ - "Access violation"显示简单 Oracle 查询字符串时出错(VS10 Exp C++)

c++ - 在 CMake 中添加自定义构建步骤

java - JNA 不支持 C++11?

c++ - 如何在没有重复代码的情况下处理 getter 的 const/non const 组合?

c++ - unix 中 select 和 poll 系统调用的功能区别

javascript - 使用 getElementById 创建表仅显示最后一个实例

function - 将多个参数传递给Powershell中的函数变量

javascript - 递增对象内部变量

c++ - 类中不允许不完整类型,但类模板中允许使用不完整类型

c++ - Qt QNetworkAccessManager总是返回空数据,状态码为0