对象已定义

Object is already defined

本文关键字:定义 对象      更新时间:2023-10-16

我正在尝试编译一个类,其中我创建了一个数据成员,其类型是另一个类"对象",但是在运行时出现问题,由于 10 个错误而失败,所有相同的错误代码,我真的一遍又一遍地解决这个问题.. 错误消息: LNK1169一个或多个乘法定义找到的符号 LNK2005 obj 中已经定义了日期和事件的一些代码 这是代码(注意:在Visual Studio 2019中,两个类的每个类分为h文件和cpp文件,不像下面显示的那样(

#pragma once
#include<iostream>
using namespace std;
class date
{
int day;
int month;
int year;
public:
date();
void readData();
~date();
};
#include"date.h"
date::date()
{
day = 0;
month = 0;
year = 0;
}
void date::readData()
{
cout << "Day: ";
cin >> day;
cout << "Month: ";
cin >> month;
cout << "Year: ";
cin >> year;
}
date::~date()
{
}

#pragma once
#include"date.cpp"
#include<string>
class Event
{
string name;
date start_date;
date end_date;
string place;
bool done;
public:
Event();
void Add();
~Event();
};
#include "Event.h"
Event::Event()
{
done = false;
}
void Event::Add()
{
cout << "Enter the event's name: ";
cin >> name;
cout << "Enter the event's start date:" << endl;
start_date.readData();
cout << "Enter the event's end date:" << endl;
end_date.readData();
cout << "Enter the event's place: ";
cin >> place;
}
Event::~Event()
{
}

您的Event标头包括:

#include"date.cpp"

这包括date的定义,而不仅仅是声明,因此包括Event.h(或任何标头的真实名称(的任何内容的结果对象文件,例如Event.cpp,将在从date.cpp本身编译的date类实现副本之上拥有自己的 类实现副本。

大概,你的意思是:

#include "date.h"

包含声明,而不将实现推入包含Event.h的每个对象。