gpt4 book ai didi

c++ - 无法使用对类型键设置映射值

转载 作者:太空宇宙 更新时间:2023-11-04 16:22:43 25 4
gpt4 key购买 nike

#include <vector>
#include <map>
#include <algorithm>
using namespace std;
class NodePoint {
public:
int x, y;
NodePoint (int i, int j){x=i; y=j;}
NodePoint (){}
bool operator == (const NodePoint &rhs) { return (x == rhs.x && y == rhs.y); };
bool operator != (const NodePoint &rhs) { return (x != rhs.x || y != rhs.y); };
};
class Link {
public:
Link (){}
};
int main() {
vector < vector <NodePoint> > nodes;
map < pair < NodePoint, NodePoint > , Link > node_links;
int sol_row_size = 10, sol_col_size = 10, i, j;
for(i=0; i<sol_row_size; i++) {
vector < NodePoint > tn;
for(j=0; j<sol_col_size; j++) tn.push_back(NodePoint(i,j));
nodes.push_back(tn);
}
for(i=0; i<sol_row_size; i++) {
for(j=0; j<(sol_col_size-1); j++) {
node_links[make_pair(nodes[i][j], nodes[i][j+1])] = Link();
}
}
return 0;
}

在上面的程序中,我只是尝试创建一个名为 node_links 的映射,其键将是一对 2 个输入节点,但我遇到了大量的编译错误(而且毫无意义我的代码中的一些行)而且我不太习惯 cpp,有什么帮助吗?我做错了什么?

编译错误如下:

$ g++ temp.cpp -o temp;
In file included from /usr/include/c++/4.6/bits/stl_algobase.h:65:0,
from /usr/include/c++/4.6/vector:61,
from temp.cpp:1:
/usr/include/c++/4.6/bits/stl_pair.h: In function ‘bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) [with _T1 = NodePoint, _T2 = NodePoint]’:
/usr/include/c++/4.6/bits/stl_function.h:236:22: instantiated from ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::pair<NodePoint, NodePoint>]’
/usr/include/c++/4.6/bits/stl_map.h:452:2: instantiated from ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::pair<NodePoint, NodePoint>, _Tp = Link, _Compare = std::less<std::pair<NodePoint, NodePoint> >, _Alloc = std::allocator<std::pair<const std::pair<NodePoint, NodePoint>, Link> >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = Link, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::pair<NodePoint, NodePoint>]’
temp.cpp:35:55: instantiated from here
/usr/include/c++/4.6/bits/stl_pair.h:209:62: error: no match for ‘operator<’ in ‘__x.std::pair<NodePoint, NodePoint>::second < __y.std::pair<NodePoint, NodePoint>::second’
/usr/include/c++/4.6/bits/stl_pair.h:209:62: note: candidates are:
/usr/include/c++/4.6/bits/stl_pair.h:207:5: note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:291:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:341:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/stl_vector.h:1290:5: note: template<class _Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
/usr/include/c++/4.6/bits/stl_tree.h:866:5: note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_map.h:899:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_multimap.h:817:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_pair.h:209:62: error: no match for ‘operator<’ in ‘__y.std::pair<NodePoint, NodePoint>::first < __x.std::pair<NodePoint, NodePoint>::first’
/usr/include/c++/4.6/bits/stl_pair.h:209:62: note: candidates are:
/usr/include/c++/4.6/bits/stl_pair.h:207:5: note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:291:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:341:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/stl_vector.h:1290:5: note: template<class _Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
/usr/include/c++/4.6/bits/stl_tree.h:866:5: note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_map.h:899:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_multimap.h:817:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_pair.h:209:62: error: no match for ‘operator<’ in ‘__x.std::pair<NodePoint, NodePoint>::first < __y.std::pair<NodePoint, NodePoint>::first’
/usr/include/c++/4.6/bits/stl_pair.h:209:62: note: candidates are:
/usr/include/c++/4.6/bits/stl_pair.h:207:5: note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:291:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:341:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/stl_vector.h:1290:5: note: template<class _Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
/usr/include/c++/4.6/bits/stl_tree.h:866:5: note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_map.h:899:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_multimap.h:817:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)

更新 1

我已将以下内容添加到 NodePOint 类

bool operator < (const NodePoint &rhs) {
return (x < rhs.x || (x == rhs.x && y < rhs.y));
};

我收到以下错误:

In file included from /usr/include/c++/4.6/bits/stl_algobase.h:65:0,
from /usr/include/c++/4.6/vector:61,
from temp.cpp:1:
/usr/include/c++/4.6/bits/stl_pair.h: In function ‘bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) [with _T1 = NodePoint, _T2 = NodePoint]’:
/usr/include/c++/4.6/bits/stl_function.h:236:22: instantiated from ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::pair<NodePoint, NodePoint>]’
/usr/include/c++/4.6/bits/stl_map.h:452:2: instantiated from ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::pair<NodePoint, NodePoint>, _Tp = Link, _Compare = std::less<std::pair<NodePoint, NodePoint> >, _Alloc = std::allocator<std::pair<const std::pair<NodePoint, NodePoint>, Link> >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = Link, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::pair<NodePoint, NodePoint>]’
temp.cpp:37:55: instantiated from here
/usr/include/c++/4.6/bits/stl_pair.h:209:62: error: passing ‘const NodePoint’ as ‘this’ argument of ‘bool NodePoint::operator<(const NodePoint&)’ discards qualifiers [-fpermissive]
/usr/include/c++/4.6/bits/stl_pair.h:209:62: error: passing ‘const NodePoint’ as ‘this’ argument of ‘bool NodePoint::operator<(const NodePoint&)’ discards qualifiers [-fpermissive]
/usr/include/c++/4.6/bits/stl_pair.h:209:62: error: passing ‘const NodePoint’ as ‘this’ argument of ‘bool NodePoint::operator<(const NodePoint&)’ discards qualifiers [-fpermissive]

最佳答案

你需要定义operator<NodePoint , 这样您的节点对就可以被排序( std::pair<NodePoint, NodePoint> 自动为您定义 operator< ,根据 NodePoint::operator< )

大概你想要这样的东西:

bool operator <(const NodePoint &rhs) const {
return (x < rhs.x || (x == rhs.x && y < rhs.y));
};

编辑添加const

关于c++ - 无法使用对类型键设置映射值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15183772/

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