0

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()
{

}
LeDYoM
  • 949
  • 1
  • 6
  • 21
  • Where is your `main()` function? What are your "10 errors?" Can you copy and paste them verbatim into the question? What are you inputting for all those `cin`'s and what's your expected output? – scohe001 Dec 02 '19 at 21:10
  • 1
    Side-note: [“using namespace std;” is considered bad practice](https://stackoverflow.com/q/1452721/364696), *especially* putting it in a header (so anything using your header "benefits" from it as well, not just one `.cpp` file that explicitly opted in). It's even worse when you're using common names like `date` that could easily exist in `std` (if not now, in the future) risking name conflicts when someone asks for `date` and the compiler can't know if they meant `std::date` or your `date` class. – ShadowRanger Dec 02 '19 at 21:13
  • If you are not able to link, how do you know you have runtime-errors? – LeDYoM Dec 02 '19 at 21:19
  • Typo. `#include "Date.h"`, not `#include "Date.cpp"`. – Peter Dec 03 '19 at 00:47

1 Answers1

2

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.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271