我编译代码时出现了一个奇怪的错误,它表示成员函数alreadys存在于另一个类中,而不是错误说明
错误LNK2005“public:void __thiscall membershipType :: print(void)” (?print @ membershipType @@ QAEXXZ)已定义 personType.obj Project1 C:\ Users \ okpal \ source \ repos \ Project1 \ Project1 \ Source.obj
以及
错误LNK1169一个或多个多重定义的符号 找到Project1 C:\ Users \ 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();
}
persontype
的源代码#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;
}
答案 0 :(得分:1)
您在第一个代码块中定义了membershipType::print()
,我认为这是从头文件中复制的。在C ++中,头文件的内容被插入到包含它们的每个文件中。据推测,您的程序中至少有两个包含此标头的源文件。当这些源文件被编译为目标文件时,两者都将包含membershipType::print()
的定义。当您尝试链接它们时,链接器将检测到两个文件都包含相同符号的定义。它不知道在哪里使用,所以它返回错误。
解决此问题的最简单方法是将membershipType::print()
的定义移至源文件。你也可以通过内联来修复它。