编译时我收到架构错误的 undefined symbol 。
必须使用 Vector、Find() 算法和非成员重载运算符函数。
我们将不胜感激有关该错误的一些指导。
#include <stdio.h>
#include <iostream>
#include <stdexcept>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
struct person
{
char firstInitial;
string firstName;
person(const char fi, const string fn)
{
firstInitial = fi;
firstName = fn;
};
char getInitial()
{
return firstInitial;
};
string getName()
{
return firstName;
};
bool operator==(const person& r);
};
bool operator==(const person& r, const person& x)
{
return x.firstInitial == r.firstInitial;
}
int main (int argc, char *argv[])
{
vector<person> myvector;
vector<person>::iterator itr;
myvector.push_back(person('j', "john"));
myvector.push_back(person('s', "steve"));
myvector.push_back(person('c', "candice"));
itr = find (myvector.begin(), myvector.end(), person('s', ""));
if (itr != myvector.end())
cout << "First Name: " << itr->getName() << '\n';
else
cout << "NOT Found" << '\n';
}
最佳答案
operator==
的声明和定义不匹配。如果你想使它成为一个非成员函数,只需删除类定义中的声明,它使它成为一个成员函数。
bool operator==(const person& r);
如果你想让它成为一个成员函数,你应该在类定义之外定义它:
bool person::operator==(const person& r)
{
...
}
关于C++ Vector Find() 重载运算符 - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37582338/