【发布时间】:2021-10-13 15:47:39
【问题描述】:
我尝试将朋友函数名称 comp 传递给 set_intersection,在 Visual Studio 2019 中编译时出现编译错误:E0020 标识符“comp”未定义
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Test {
friend bool comp(const Test& A, const Test& B) { return A.a < B.a; }
public:
int a{ 10 };
};
int main() {
vector<Test> v1{ Test{} };
vector<Test> v2{ Test{} };
vector<Test> rs;
set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(rs), comp);
}
但是,如果我将 comp 更改为 operator<,它可以与 set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(rs)); 正常工作。为什么comp 不起作用?
【问题讨论】: