【发布时间】:2015-11-16 09:40:04
【问题描述】:
我正在实施一种回溯算法来解决数独问题,但我在使用地图时遇到了一些问题。
谁能告诉我为什么在编译我的代码时会出现这两个错误?第一个错误是关于在地图上声明迭代器的行。第二个是在做Adj[&node] = mList;
错误:
错误 C2664 'std::_Tree_const_iterator>>> std::_Tree>::find(Node *const &) const' : 无法将参数 1 从 'const Node *' 转换为 'Node *const &'
错误 C2679 '[' 二进制:未找到接受“const Node *”类型的右操作数的运算符(没有可接受的转换)
(我的 Visual Studio 是法语的,所以我翻译了错误消息。希望这样没问题)
我得到错误的代码:
template<class T>
void AdjList<T>::addElement(const Node<T>& node, const vector<Node<T>>& vecOfNeighbours) {
typename map< Node<T>*, LinkedList<T>>::iterator mit = Adj.find(&node);
if (mit!=Adj.end()) {
LinkedList<T> mList;
for (typename vector<Node<T>>::const_iterator it = vecOfNeighbours.begin(); it != vecOfNeighbours.end(); it++) {
mList.Add((*it).getValue()[0]);
}
Adj[&node] = mList;
}
}
我的班级定义:
template<class T>
class AdjList
{
private:
map<Node<T>*, LinkedList<T>> Adj;
public:
AdjList();
AdjList(const AdjList<T>& adjlist);
AdjList(const Node<T>& node, const vector<Node<T>>& vecOfNeighbours);
void addElement(const Node<T>& node, const vector<Node<T>>& vecOfNeighbours);
void Print() const;
};
template<class T>
class LinkedList
{
Node<T>* head;
int getEndList();
Node<T>* returnFrontEnd(void) const;
public:
LinkedList();
LinkedList(T data);
LinkedList(const LinkedList<T>& list);
LinkedList<T>& operator=(const LinkedList<T>& list);
void Add(T data);
void AddAt(int index, T data);
void Print();
~LinkedList();
};
template<class T>
class Node
{
protected:
vector<T> _value;
Node<T>* child;
public:
Node(T value);
void addValue(T value);
Node<T>* clone() const;
Node(const Node<T>& node);
vector<T> getValue() const;
Node<T>* returnChild() const;
void AddChild(const Node<T>& node);
Node& operator=(const Node<T>& node);
~Node();
};
【问题讨论】: