C++ Vector Find() 重载运算符 - 错误

标签 c++ vector find operator-overloading

编译时我收到架构错误的 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/

相关文章:

c++ - 在派生函数中指定 void* 参数

c++ - 返回 1 个 UTF-8 字符的功能?

c++ - 在静态断言中引用此指针?

c++ - C++ 容器内的 char* 作用域

r - 重叠元素的数量

bash - 列出与给定模式匹配的所有文件

c++ - 如何通过结构属性搜索结构 vector ?

安卓图像映射。 - 显示 .svg 并将其用作图像映射(触摸区域)

regex - Notepad++ regex 如何替换第三次出现的斜杠并绕过第一个和第二个斜杠?

arrays - 在工作表中搜索相似行以组合在一起 Excel VBA