0
我已经写了一个模板函数来查找两个变量之间的最大值。当输入两个字符串时它工作正常。然后我有一个创建包含字符串的“节点”的类。我正在尝试在类中编写一个overloader函数,以便>运算符可以识别这些节点。Overloading>运算符来识别包含字符串的节点
这是我的模板函数和我的节点类。向前以下行斜杠是扔在编译的时候出现错误:
template<typename T>
T maximum(const T& a, const T& b){
return a > b ? a : b;
}
class Node{
public:
Node(const string& s = "Default"):
data(s){
}
string get_data(){
return this->data;
}
friend ostream& operator<<(ostream& os, vector<Node> &v){
for(int i = 0; i < v.size(); i++){
os << v[i].get_data() << ", ";
}
cout << endl;
return os;
}
friend bool operator>(const Node& a, const Node& b){
/////////////////////////////////////////////////////////////////////
if(a.get_data() > b.get_data()){
return true;
}
else return false;
}
private:
string data;
Node* next;
};
为什么不能在我的GET_DATA()函数>操作员的工作量?
您正在调用const引用的非const函数。将get_data函数更改为'string get_data()const {return this-> data; }'。 BTW发布你将来得到的错误信息。 –
我知道这是来自错误消息的consts问题,但我甚至没有想到get_data函数。我是C++的新手,发现编译器错误很难理解。谢谢。 – KOB
@KOB此外,C++程序员这样做的方式是重载'operator <'和'operator ==',并根据这两者编写其他运算符。 – PaulMcKenzie