Reputation: 11
I am trying to compile a class in which I created a data member that its type is of another class "objects", but something goes wrong during run time and it fails due to 10 errors all of the same error code and I really worked on this problem over and over .. The errors messages : LNK1169 one or more multiply defines symbols found LNK2005 some code of date and Event are already defined in obj Here is the code ( Note: in visual studio 2019, each class of the two classes is separated into h file and cpp file not like it appears below)
#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()
{
}
Upvotes: 0
Views: 392
Reputation: 155487
Your Event
header includes:
#include"date.cpp"
That's including the definitions of date
, not just the declarations, so the resulting object file for anything including Event.h
(or whatever the real name of that header is), e.g. Event.cpp
, will have its own copy of the date
class implementation, on top of the one compiled from date.cpp
itself.
Presumably, you meant to do:
#include "date.h"
to include the declarations without shoving the implementation into every object that includes Event.h
.
Upvotes: 2