Problem troubleshooting my "date" class

Hi,

I am having some issues to print some data in my program. I have created this class which takes in the day, month and year and will return the indvidual values, overload the "==" operator and perform a display of the data in the form "dd/mm/yy". I am having the error of "multiple definition of 'Date::Date(unsigned short, unsigned short, unsigned short)" in my source file. Could someone please tell me what am I doing wrong ? Thank you in advance.

Header file (date.h)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef DATE_H
#define DATE_H


class Date
{
private:
    unsigned short day, month, year;

public:
    Date();
    Date(unsigned short, unsigned short, unsigned short);
    unsigned short getday();
    unsigned short getmonth();
    unsigned short getyear();
    void display();
    bool operator==(Date);



    ~Date();
};

#endif // DATE_H



Source file (date.cpp)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "date.h"
#include <iostream>

using namespace std;

Date::Date(unsigned short d, unsigned short m, unsigned short y )
{
   day = d; month = m; year = y;
}

Date::Date()
{}

unsigned short Date::getday()
{
    return(day);
}

unsigned short Date::getmonth()
{
    return(month);
}

unsigned short Date::getyear()
{
    return(year);
}

void Date::display()
{
  cout << day << "/" << month << "/" << year;

}

bool Date::operator==(Date c)
{
   return((day == c.day ) && (month == c.month) && (year == c.year));

}


Date::~Date()
{}


On W7 I can compile it with no errors and no warnings by the following command:
g++ -std=c++2a -Werror -Wall -Wextra -Wpedantic -O2 date.cpp main.cpp -o main.exe

I am using Qt and Windows 10 and I am not able to compile without the error mentioned above. The error highlits all the functions created in the source file. But thank you for your answer anyways Enoizat.
Last edited on
I am using Qt

This is probably a stupid question, but...
Are you sure you've created a Non-Qt Project --> Plain C++ Application?
Would you like to post your .pro file, so that we can check it?
Hi I hope with my main.cpp file is enough since I cannot insert my file here. I can always give a link to my drive if required:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#include <iostream>
#include "date.h"
#include "date.cpp"

using namespace std;

int main()
{
    unsigned short d, m, y;
    cout << "Hiya, Could you please enter a day, month and year ?" << endl;
    cout << "\nDay ? ";
    cin >> d;
    cout << "\nMonth ?";
    cin >> m;
    cout << "\nYear ?";
    cin >> y;

    Date c1(d,m,y);
    cout << "\n\nOk. Just to confirm. The values you have entered are: " << endl;
    cout << "\nDay: " << c1.getday();
    cout << "\nMonth: " << c1.getmonth();
    cout << "\nYear: " << c1.getyear();
    cout << "Proper display: ";
    c1.display();

    Date c2(d,m,y);
    cout << "\n\nCould you please enter a new day, month and year ?" << endl;
    cout << "\nNew day ? ";
    cin >> d;
    cout << "\nNew month ?";
    cin >> m;
    cout << "\nNew year ?";
    cin >> y;

    cout << "\n\nOk. Just to confirm. The new values you have entered are: " << endl;
    cout << "\nNew day: " << c2.getday();
    cout << "\nNew month: " << c2.getmonth();
    cout << "\nNew year: " << c2.getyear();
    cout << "Proper display: ";
    c2.display();

    if(c1==c2)
      cout<<"\n\nBoth dates are exactly the same."<<endl<<endl;







    return 0;

}
The problem is probably that you're #including your source file instead of adding it to your project to be compiled.

Hi jlb. I have been doing this in all my previous classes exercises and it has been working for me. That is how my lecturer showed me and how I have done it for all my exercise so far. But thank you for your reply.
I don't care how you have been doing it, you need to add the source file to your project not #include the file. The #inclusion is the reason for your current problems.

Wow you were actually right. By putting both my header and source files into my main file it actually worked. Thank you very much. But why did the #include give me that problem ? Both the header and source files are in the same directory as my main.cpp and I have programmed in this way in previously without encountering this error ?

Why did it happen here ?
Do you understand how the #include mechanism works?

By the way it would have worked if you just put the contents of the source file (.cpp) in your main file and left the .h file as a separate file, or if you would have just added the source file to your project to be compiled instead of copied.

Honestly, I only understand the #include to call header and source files. No more than that or how it works internally. But still why has this exercise given me this error whereas other classes exercises didn't ? What did I write here that did not allow me to do as in previous programs I have coded with #include ? I am just curiious about it and I need to know for any future programs I make.
Last edited on
Honestly, I only understand the #include to call header and source files.

No you really don't know anything about #include. Perhaps you should try to research how #include actually works in C++.

But still why has this exercise given me this error whereas other classes exercises didn't ?


Because you got extremely lucky with your previous crap.

What did I write here that did not allow me to do as in previous programs I have coded with #include ?

"#include <date.cpp>"

Also exactly what did your compiler tell you was the problem? Your error messages should tell you exactly where the problem was detected. If it was a linker error then those messages should also tell you more about the problem as well.

I am just curiious about it and I need to know for any future programs I make.

Just remember that until you start trying to use templates you need to add your source files to your project and *never #include them.

"*never" This doesn't apply to templates.





Topic archived. No new replies allowed.