【发布时间】:2019-03-28 22:00:32
【问题描述】:
我遇到了问题,我不知道哪里出了问题。我需要为我的班级重载运算符+,以便可以合并两个或更多列表。
错误
Xcode 一直在说:
二进制表达式的无效操作数('List *' 和 'List *')
我的代码
template <typename Type> class List {
public:
Type data;
List *next;
void set_head(Type d) {
data = d;
next = nullptr;
}
void int_print() {
cout << data << endl;
}
};
template <typename Type>
List<Type>* operator+ (List<Type> *head1, List<Type> *head2) {
List<Type> *tmp = head1, *headf = nullptr, *tmpf = nullptr;
tmpf = list_create_head(tmp, tmp->data);
headf = tmpf;
tmp = tmp->next;
while (tmp != nullptr) {
tmpf = list_create_continue(tmpf, tmp->data);
tmp = tmp->next;
}
tmp = head2;
while (tmp != nullptr) {
tmpf = list_create_continue(tmpf, tmp->data);
tmp = tmp->next;
}
return headf;
}
//problem occurs here:
else if ((c == 8) * (bonus != nullptr)) {
List<int> *mem = nullptr;
mem = head + bonus; //Here!
free(bonus);
cout << "Result of merging: " << endl;
tmp = mem;
while (tmp != nullptr) {
tmp->int_print();
tmp = tmp->next;
}
free(mem);
}
【问题讨论】:
-
完全不相关:如果您学习 the Rule of Three (and friends) 和 use
new(and pointers) a lot less often,您可能会发现使用 C++ 的时间会轻松很多。 -
就术语而言,此代码称为
List的内容通常称为Node。List将具有管理Node对象以维护列表的代码。
标签: c++ list operator-overloading