【发布时间】:2020-10-09 14:12:10
【问题描述】:
我需要使用重载== 检查这两个向量是否相等,但它始终只工作else 语句。
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
class Set {
public:
vector<int> mult;
friend bool operator ==(const Set&, const Set&);
};
bool operator ==(const Set& a, const Set& b) {
return (a.mult == b.mult);
}
int main()
{
Set* set_1 = new Set;
Set* set_2 = new Set;
for (int i = 1; i <= 5; i++)
set_1->mult.push_back(i);
for (int i = 1; i <= 5; i++)
set_2->mult.push_back(i);
if (set_2 == set_1) {
cout << "True ";
}
else {
cout << "False";
}
}
【问题讨论】:
-
set_1是Set*,而不是Set。你为什么要使用set_1和set_2的指针?投票结束是一个错字。摆脱指针。 -
那么如何分配动态内存呢?
-
这里为什么需要动态分配?
-
这是我的任务要求
-
你能发布你的要求吗?他们很可能希望
Set使用动态分配,而不是main
标签: c++ overloading operator-keyword