我只是在学习如何使用 C/C++ 并试图编写一个程序,其中“otherPerson”(名字,姓氏)从“person”(名字)继承。我被困在人的 compareTo 函数上。它应该按名字的字母顺序对指向人的指针数组进行排序。(不确定我是否正确表达了这个想法)。最终目标是打印数组的排序内容。
我不断得到:
错误 LNK2019:函数 _main 1>c:\users\laur\documents\visual studio 2012 中引用的未解析的外部符号“void __cdecl compareTo(class person * * const,int)”(?compareTo@@YAXQAPAVperson@@H@Z) \Projects\Project1\Debug\Project1.exe : 致命错误 LNK1120: 1 unresolved externals
当我尝试构建时。我在互联网上查看了更多信息,但我很确定我的包含语句是正确的。该函数旨在作为成员函数。
测试.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;
}
}
}