c++ - 为什么我的编译器找不到我的函数?

标签 c++ arrays

我正在学习如何使用 C/C++,并正在尝试编写一个程序,其中“otherPerson”(名字、姓氏)继承自“person”(名字)。我坚持使用 person 的 compareTo 函数。它应该按名字的字母顺序对指向人的指针数组进行排序。 (不确定我是否正确表达了这个想法)。最终目标是打印数组的排序内容。

我不断得到:

error LNK2019: unresolved external symbol "void __cdecl compareTo(class person * * const,int)" (?compareTo@@YAXQAPAVperson@@H@Z) referenced in function _main 1>c:\users\laur\documents\visual studio 2012\Projects\Project1\Debug\Project1.exe : fatal error LNK1120: 1 unresolved externals

当我尝试构建时。我在互联网上四处寻找更多信息,但我很确定我的 include 语句是正确的。该函数旨在作为成员函数。

测试.cpp:

#include "stdio.h"
   #include "otherPerson.h"
   #include<iostream>
   #include <string>
   using namespace std;
      void compareTo(person *array[7], int );
       int main(){
           int length = 7;
           person* epicJourney[6];
          //fill array
           compareTo(epicJourney, length);

人.h:

#include <stdio.h>
#include <string>
using namespace std;

class person {
    protected:
        string firstName;
    public: 
        person(string firstName);
        virtual void setFirstName(string firstName);
        virtual string getFirstName();  
        virtual void compareTo (person *array[7], int length);
        virtual string toString();
};

人.cpp:

#include "person.h"
#include <string>
using namespace std;
person::person(string firstName){
    this->firstName = firstName;
}

void person::setFirstName(string aName){
    firstName =aName;
}

string person::getFirstName(){
    return ((*this).firstName);
}

string person::toString(){
    return (this->firstName);
}

void person::compareTo(person *array[], int length){
    int i;
    int j;

    person *current;

    for (i=1; i<length; i++){
        current = array[i];
        for (j=i; j>=1 && (current < array[j-1]); j--){
            array[j] = array[j-1];
            array[j-1] = current;
        }

    }
}

最佳答案

您的原型(prototype)是 void compareTo(...); 但您的实际实现是 void person::compareTo(...) 。你要拿定主意:要不要类成员函数?

关于c++ - 为什么我的编译器找不到我的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11142636/

相关文章:

java - 将字符串拆分为多个数组

c++ - 如何从excel文件中提取数据到c?

c++ - Hippo Mocks 中具有不同返回值的多个预期调用是否可以重复使用模拟?

c++ - 无法理解为什么我的程序会抛出错误

java - 如何获取 C[] 的类对象,给定 C 的类对象

c++ - 如何拥有灵活的嵌套初始化器?

javascript - 简单地遍历一个数组

javascript - 分组数组内的编号

C++ 部分特化不适用于不同大小的特征矩阵

c++ - 关闭控制台窗口时优雅退出