【问题标题】:I am getting errors in the xfunction built in class and i dont know what to do我在类内置的 xfunction 中遇到错误,我不知道该怎么做
【发布时间】:2013-04-25 02:00:15
【问题描述】:

我正在创建一个使用 set 来保存对值的地图数据结构。我为该程序制作了一个自定义配对类。我完成了大部分调试,现在我在内置的 xfunction 类中遇到了这些错误。

错误 6 错误 C2784: 'bool std::operator &,const _Elem *)' : 无法推断出 'const std::basic_string<_elem> &' 来自 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

错误 7 错误 C2784: 'bool std::operator &)' : 无法从 'const 推断出'const _Elem *' 的模板参数对'c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

错误 8 错误 C2784: 'bool std::operator &,const std::basic_string<_elem> &)' : 无法推断模板来自 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125 的 'const std::basic_string<_elem> &' 的参数

错误 9 错误 C2784: 'bool std::operator &,const std::move_iterator<_ranit2> &)' : 无法推断出 'const std::move_iterator 的模板参数<_ranit> &' 来自 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

错误 10 错误 C2784: 'bool std::operator &,const std::_Tree<_traits> &)' : 无法推导出 'const std::_Tree 的模板参数<_traits> &' 来自 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

错误 11 错误 C2784: 'bool std::operator &,const std::list<_ty> &)' : 无法推断出 'const 的模板参数std::list<_ty> &' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

错误 12 错误 C2784: 'bool std::operator &,const std::unique_ptr<_ty2> &)' : 无法推断出 'const 的模板参数std::unique_ptr<_ty> &' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

错误 13 错误 C2784: 'bool std::operator &,const std::reverse_iterator<_ranit2> &)' : 无法推导出 'const std::reverse_iterator 的模板参数<_ranit> &' 来自 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

错误 14 错误 C2784: 'bool std::operator &,const std::_Revranit<_ranit2> &)' : 无法推断出 'const 的模板参数std::_Revranit<_ranit> &' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

错误 15 错误 C2784: 'bool std::operator &,const std::pair<_ty1> &)' : 无法推断出 'const 的模板参数std::pair<_ty1> &' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

错误 16 错误 C2676: 二进制 '

这是我的 map2.h 代码

#ifndef MAP_H_2
#define MAP_H_2
#include <list>
#include <set>
#include <utility>
#include <iterator>
#include <iostream>
#include <string>
using namespace std;

//pair class header
template<typename F, typename S>
class Pair
{
public:
    Pair(const F& a, const S& b);
    Pair();
    F get_first() const;
    S get_second() const;
private:
    F first;
    S second;
};
//pair class definitions
template<typename F, typename S>
inline Pair<F, S>::Pair(const F& a, const S& b):first(a),second(b){}

template<typename F, typename S>
inline Pair<F, S>::Pair()
{
}

template<typename F, typename S>
inline F Pair<F, S>::get_first() const
{
    return first;
}

template<typename F, typename S>
inline S Pair<F, S>::get_second() const
{
    return second;
}
//map header
class map2
{
public:
    map2();
    void at_put(string key, int value);
    Pair<string, int> at(string key);
    bool contain_key(string key);
    int value_of(string key);
    void remove_key(string key);
    void print();

private:
    set<Pair<string, int>> theList;
};

//map definition
map2::map2(){}

void map2::at_put(string key, int value)
{
    bool notThere = true;
    if(contain_key(key))
    {
        notThere = false;
    }
    if(notThere)
    {
        Pair<string, int> thePair(key, value);
        theList.insert(thePair);
    }
}

Pair<string, int> map2::at(string key)
{
    set<Pair<string, int>>::iterator iter = theList.begin();
    Pair<string, int> thePair;
    string temp;
    for(int x = 0; x<theList.size() ; x++)
    {
        thePair = *iter;
        temp = thePair.get_first();
        if(!key.compare(temp))
        {
            return thePair;
        }
        iter++;
    }
    Pair<string, int> noPair = Pair<string, int>("none", -1);
    return noPair;
}

bool map2::contain_key(string key)
{
    set<Pair<string, int>>::iterator iter = theList.begin();
    Pair<string, int> thePair;
    string temp;
    for(int x = 0; x<theList.size() ; x++)
    {
        thePair = *iter;
        temp = thePair.get_first();
        if(!key.compare(temp))
        {
            return true;
        }
        iter++;
    }
    return false;
}

int map2::value_of(string key)
{
    set<Pair<string, int>>::iterator iter = theList.begin();
    Pair<string, int> thePair;
    string temp;
    for(int x = 0; x<theList.size() ; x++)
    {
        thePair = *iter;
        temp = thePair.get_first();
        if(!key.compare(temp))
        {
            return thePair.get_second();
        }
        iter++;
    }
    return NULL;
}

void map2::remove_key(string key)
{
    set<Pair<string, int>>::iterator iter = theList.begin();
    Pair<string, int> thePair;
    string temp;
    for(int x = 0; x<theList.size() ; x++)
    {
        thePair = *iter;
        temp = thePair.get_first();
        if(!key.compare(temp))
        {
            theList.erase(iter);
        }
        iter++;
    }
}

void map2::print()
{
    set<Pair<string, int>>::iterator iter = theList.begin();
    Pair<string, int> thePair;
    string temp;
    int temp2;
    for(int x = 0; x<theList.size() ; x++)
    {
        thePair = *iter;
        temp = thePair.get_first();
        temp2 = thePair.get_second();
        cout << "Key:" << temp << " Value:" << temp2 << "\n";
        iter++;
    }
}

#endif

这是我的具有主要功能的代码

#include "map2.h"
#include <string>
#include <iostream>

using namespace std;


int main()
{
    map2 theMap;
    theMap.at_put("John", 1000);
    theMap.at_put("Chris", 1000);
    theMap.at_put("John", 1500);
    theMap.at_put("Bob", 1250);

    theMap.print();

    theMap.remove_key("bob");

    theMap.print();

    string findKey;
    cout << "please enter a key to remove" << "\n";
    cin >> findKey;

    bool keyFound = theMap.contain_key(findKey);

    if(keyFound)
    {
        cout << "We found it! The value is:" << theMap.value_of(findKey) << "\n";
    }
    else
    {
        cout << "we don’t have this key " << findKey << "in the map" << "\n";
    }


}

【问题讨论】:

    标签: c++ stl compiler-errors


    【解决方案1】:

    问题是这样的:

    error C2676: binary &lt; : const Pair 未定义此运算符或转换为预定义运算符可接受的类型

    没有为map2.hPair 类定义&lt; 运算符。 C++ STL 集类是使用平衡搜索树实现的,例如Red-black tree。集合中的所有元素都存储在二叉树中。该集合通过将一个元素与树的根进行比较来检查它是否包含一个元素,如果没有匹配,则仅递归到一个适当的子树。这需要集合元素的比较运算符,以确定树的哪一侧包含寻找的元素。

    此要求隐含在documentation for set 中。

    不幸的是,像这样的 C++ STL 编译器消息通常是非常无用的。久而久之你会习惯的。有些事情你可以试着理解它们:

    • 只考虑包含您编写的文件的错误消息,而忽略有关标准库中文件的错误消息。在这种特殊情况下,这不会有帮助,但有时会有所帮助。
    • 关注第一条或最后一条错误消息并尝试理解它
    • 尝试使用 GCC 或 Clang 等其他编译器对其进行编译,看看错误消息是否更有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 1970-01-01
      • 2022-11-05
      • 1970-01-01
      • 1970-01-01
      • 2019-02-11
      • 2022-01-08
      相关资源
      最近更新 更多