我在弄清楚为什么我无法得到std :: vector :: erase使用我自己的类对象的矢量时遇到了一些麻烦。下面的代码抛出一个“没有可行的重载‘=’”错误,并不能找出为什么一些广泛的溢出搜索/ tutorialspoint后/ ...当使用vector :: erase时,没有可行'='
我的类定义“MyClass.hpp”:
#include <string>
#include <vector>
class node;
class graph{
public:
graph();
std::vector<node> nodeList;
};
class node{
public:
node(std::string name) : name(name){};
void operator=(node& rhs);
std::string name;
std::vector<node> adjacent;
};
void node::operator=(node& rhs){
name = rhs.name;
adjacent = rhs.adjacent;
}
和我的主文件:
#include "MyClass.hpp"
int main(int argc, const char * argv[]) {
node node1("root"), node2("leaf");
node1.adjacent.push_back(node2);
node2.adjacent.push_back(node1);
node1.adjacent.erase(node1.adjacent.begin());
graph myGraph;
return 0;
}
这是完全可能的载体实现不具有不完全类型的新的支持(具有节点的载体的节点)。 – chris
你的'operator ='坏了。赋值运算符必须采取'const'参考作为参数,并返回一个可变的参考,即,它应该是'节点&节点::运算符=(const的节点&RHS)',并返回'* this'。 –
为什么你甚至自己定义'operator ='?编译器生成的会更好。见规则零。 – aschepler