未知功能已经存在错误

unknown function already exists error

本文关键字:存在 错误 功能 未知      更新时间:2023-10-16

我编译代码时会出现一个奇怪的错误,它说成员函数AlReadys存在于另一个类中,该类别没有错误说

错误lnk2005" public:void __thiscall会员type :: print(void)" (?print@membershipType @@ qaexxz)已经定义 pershytype.obj project1 c: users okpal source repos project1 project1 project1 source.obj

以及

错误lnk1169一个或多个倍数定义的符号 找到Project1 C: User Okpal source repos project1 debug project1.exe 1

我想知道是否有人可以帮助找出错误我的班级代码在下面

#include <iostream>
#include <string>
using namespace std;
class addressType {  //class defintions and prototypes member variables
public:
    addressType();
    string streetAddressNum, streetName, streetType, city, stateInitials;
    int zipCode;
};
class personType
{
public:
    personType();
    string firstName;
    string lastName;
    int personNum;
    char gender;
    int personID;
    addressType address;
    void setInterest1(string interest1);//mutator
    void setInterest2(string interest2);
    void printPerson();
    string  GetInterest1() const;    // Accessor
    string  GetInterest2() const;
private:
    string SetInterest1;
    string SetInterest2;
};
//define membershipType class
class membershipType :public personType
{
public:
    char membership_type;
    char membership_status;
    membershipType();  // 1st constructor
    membershipType(char, char);  // 2nd constructor
    void print();
};
void membershipType::print() 
{
    cout << GetInterest1();
}

pershertype的源代码

#include "personType.h"
personType::personType()
{
    int personNum = 0;
    int personID = 0;
}
addressType::addressType() {
    int zipCode = 0;
}
void personType::setInterest1(string interest1) {
    SetInterest1 = interest1;
}//mutator
void personType::setInterest2(string interest2) {
    SetInterest2 = interest2;
}
string personType::  GetInterest1() const
{
    return SetInterest1;
}// Accessor
string personType:: GetInterest2() const {
    return SetInterest2;
}
void personType :: printPerson() {//constructor
    cout << firstName << " " << lastName << " " << gender << " " <<
        personID << " " << address.streetAddressNum << " "
        << address.streetName << " " << address.streetType
        << " " << address.city << " " << address.stateInitials
        << " " << address.zipCode << " " << SetInterest1 << " " << SetInterest2 << endl;
}

您在第一个代码块中具有membershipType::print()的定义,我认为我可以从标题文件复制。在C 中,将标题文件的内容插入到包含它们的每个文件中。据推测,您的程序中至少有两个源文件,其中包括此标头。当这些源文件被编译到对象文件时,两者都将包含membershipType::print()的定义。当您尝试链接它们时,链接器将检测到两个文件都包含相同符号的定义。它不知道该在哪里使用,因此返回错误。

解决此问题的最简单方法是将membershipType::print()的定义移至源文件。您也可以通过内联。

修复它。