对象已定义
Object is already defined
我正在尝试编译一个类,其中我创建了一个数据成员,其类型是另一个类"对象",但是在运行时出现问题,由于 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
的每个对象。
相关文章:
- 自定义对象的dlib序列化在gcc中失败
- 如何将带有自定义对象的容器从C++传递到QML
- 直接在 unordered_map 的方法中使用哈希,而不是生成哈希的用户定义对象
- 如何使用Q_PROPERTY公开自定义对象列表
- 如何在自定义对象的<<运算符中添加自定义前缀
- 将自定义对象作为参数从目标 C 传递到 C++
- 如何使用构造函数声明指向用户定义对象的指针?
- 使用自定义对象声明shared_ptr数组时出现错误 C2664
- 在具有自定义对象的集合中查找值
- std::set 不会检测到重复的自定义对象
- 如何使用自定义对象的序列化在 c++ 中编写自定义二进制文件处理程序
- std::排序为排序自定义对象时出现的向量引发错误
- 如何通过引用对用户定义对象的类型集 (STL) 的数组元素进行增强迭代?
- 具有自定义对象的C 优先级队列
- 自定义对象构造函数在循环外部循环
- 带有自定义对象的C 范围循环
- 为什么不允许通过 decltype(lamda) 定义对象,我该如何改进它?
- 如何正确地将规则 5(或零?)应用于包含带有字符串的自定义对象向量的类
- 编写安全包装类以管理用户定义对象的指针
- 如何在包含自定义对象的容器中使用分区函数C++