0

date.h file:

 #include <iostream>
    using namespace std;
    
(3)    class Date {
    private:
        string date;
        string start_time;
        string stop_time;
    public:
        Date();
        Date(Date&);
        Date(string, string, string);
               //overloaded constructor;
        void set_date(string);
        void set_start_time(string);
            //void check_validity(string, string, string);
        void set_stop_time(string);
        string get_date();
        string get_start_time();
        string get_stop_time();
        void input();
        void print();
    
        ~Date();
    };

date.cpp file:

#include <iostream>
#include "date.h"
using namespace std;
Date::Date()
{
    //default constructor
    date = '\0';
    start_time = '\0';
    stop_time = '\0';
}
Date::Date(Date& d)
{
    //copy constructor
    date = d.date;
    start_time = d.start_time;
    stop_time = d.stop_time;
}
Date::Date(string date, string start_time, string stop_time)
{
    this->date = date;
    this->start_time = start_time;
    this->stop_time = stop_time;
}
void Date::set_date(string date)
{
    this->date = date;
}
void Date::set_start_time(string start_time)
{
    this->start_time = start_time;
}
void Date::set_stop_time(string stop_time)
{
    this->stop_time = stop_time;

}
string Date:: get_date()
{
    return this->date;
}
string Date::get_start_time()
{
    return this->start_time;
}
string Date::get_stop_time()
{
    return this->stop_time;
}
void Date::input()
{

    cout << "enter the date of the wedding event=";
    cin >> date;
    cout << endl;
    cout << "Enter the starting time of the wedding cermony=" << endl;
    cin >> start_time;
    cout << endl;
    cout << "Enter the ending time of the wedding cermony=" << endl;
    cin >> stop_time;
    cout << endl;

}
void Date::print()
{
    cout << "The Date of the wedding event is=" << this->date << endl;
    cout << "The Starting time of the weeding event is" << this->start_time << endl;
    cout << "The End time of the weeding event is" << this->stop_time << endl;
}
Date::~Date()
{
    //destructor
    date = '\0';
    start_time = '\0';
    stop_time = '\0';
}

main.cpp

#include <iostream>
 #include <cstdlib>
#include "date.h"
#include "event.h"
#include "meeting.h"
#include "contact.h"
#include "appointment.h"
#include "calender.h"
using namespace std;
int main()
{
    Date d;
    d.set_date("15-12-2002");
    d.set_start_time("03:00 PM");
    d.set_stop_time("07:00 PM");
    Calender C;

    Event* A = new Appointment("0342-1447420", d,"wedding cermony");

    cout<<A->toString();
    Event* B = new Meeting("0342-1447421", d,"Disussion");
    cout<<B->toString();
    Calender c;
    c.addevent(A);
    c.addevent(B);
    int count = 0;
    Event** e = c.findEventsByKeyword("Disussion",count);
    cout<<e[0]->get_title();
    system("pause");
    return 0;
}

I am getting this error:

redefinition of class Date in date.h file at line number 3.

I try my best to solve it but I failed. What is wrong in it? It is in C++. Here I am attaching picture of error:

error picture

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Do you have a `main.cpp` file that also `#include "date.h"`? – justANewb stands with Ukraine Nov 08 '21 at 13:32
  • 3
    You probably want to add header guards. Related: [https://stackoverflow.com/questions/4767068/header-guards-in-c-and-c](https://stackoverflow.com/questions/4767068/header-guards-in-c-and-c) – drescherjm Nov 08 '21 at 13:33
  • FYI - `Date` doesn't need a user-defined copy constructor or user-defined destructor. The more needless code you write, the more chance of bugs to occur. Also, `std::string` already defaults to an empty string. There is no need to assign `\0` to the string member variables. – PaulMcKenzie Nov 08 '21 at 13:34
  • you posted too much and too little code. Please read about [mcve]. Maybe you include `date.h` twice? But not in the posted code – 463035818_is_not_an_ai Nov 08 '21 at 13:37
  • Also, in the future, [please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Amadan Nov 08 '21 at 13:39
  • i have put main.cpp in code. please check that – Sajid Ehsan Nov 08 '21 at 13:40
  • 3
    I suspect than at least one of event.h, meeting.h, contact.h, appointment.h, calender.h includes (directly or indirectly) date.h and you don't have [header_guard](https://en.cppreference.com/w/cpp/preprocessor/include#Notes) (or `#pragma once`). – Jarod42 Nov 08 '21 at 13:44
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Nov 09 '21 at 22:05

1 Answers1

0

Probably because in one of these header files:

#include "event.h"
#include "meeting.h"
#include "contact.h"
#include "appointment.h"
#include "calender.h"

You have #include "date.h"

Consider this example:

foo.h

class Foo
{
    int foo1;
};

bar.h

#include "foo.h"

main.cpp

#include "foo.h"
#include "bar.h"
int main()
{
    Foo foo;
}

First, main.cpp #include "foo.h", which copies the definition for class Foo into main.cpp. Then main.cpp #include "bar.h", which #include "foo.h" itself. This copies contents of #include "foo.h" (including the definition for class Foo) into #include "bar.h" which then gets copied into main.cpp.

To resolve this, you have to use header guards, e.g in foo.h:

#ifndef FOO_H
#define FOO_H
class Foo
{
    int foo1;
};
#endif FOO_H