When you get a long list of errors you should always start with the first one in the list. Later errors might not be real errors but might just be side effects of earlier errors. After fixing the first error you recompile and repeat until you have no errors.
___
http://cplusplus.com/forum/general/283271/#msg1226230 ___
When I compile the code in your first post with GCC I get the following error as the first:
1 2 3 4 5 6
|
class Date
{
...
FirstDate(int month, int day, int year); // error: ISO C++ forbids declaration of ‘FirstDate’ with no type
...
};
|
The problem here is that the constructor is not named the same as the class. So the compiler probably thinks you are trying to define a normal member function and therefore complains when it doesn't see a type name in front of the function name.
After fixing this error I get another error:
1 2 3 4 5 6 7 8 9 10
|
#ifndef Date
#define Date
class Date
{
...
Date(int month, int day, int year); // error: expected unqualified-id before ‘int’
...
};
#endif
|
which to me didn't make a lot of sense at first. But then I notice your include guard is named Date, the same name as your class. This will not work. You have to name it something unique that is not used in the rest of your program. A relativity common convention is to spell the file name in upper case letters and replace dots with underscore so Date.h becomes DATE_H.
1 2 3 4
|
#ifndef DATE_H
#define DATE_H
...
#endif
|
After making this change the code in the header compiles without errors.
___
http://cplusplus.com/forum/general/283271/#msg1226231 ___
After the earlier fixes the code in your second post only shows me the following error:
1 2 3
|
using namespace std;
{ // error: expected unqualified-id before ‘{’ token
...
|
This { should just not be there. After removing it a few more errors pop up:
Same problem as earlier with incorrectly named constructor:
1 2 3 4
|
Date::FirstDate() // error: ISO C++ forbids declaration of ‘FirstDate’ with no type
{
...
}
|
Missing semicolon:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
void Date::setNames(int month, string monthName)
{
switch(month)
{
case '1' : monthName = "January";
break;
case '2' : monthName = "February" // error: expected ‘;’ before ‘break’
break;
case '3' : monthName = "March";
break;
...
}
}
|
After fixing these issues I get the following error:
1 2 3 4
|
Date::Date() // error: no declaration matches ‘Date::Date()’
{
...
}
|
The problem here is that in the header you have declared the constructor to take three ints as argument but here you are defining it to take no arguments at all. You either have to change the declaration or definition so that they match, or add both constructors.
Note that this is not correct:
1 2 3 4 5 6 7
|
Date::Date()
{
int month = 1;
int day = 1;
int year = 1;
string monthName = "January";
}
|
You are declaring local variables that goes out of scope when the constructor ends. This will not affect the member variables with the same name.
Simply remove the type names
1 2 3 4 5 6 7
|
Date::Date()
{
month = 1;
day = 1;
year = 1;
monthName = "January";
}
|
or use the constructor initialization list
1 2 3 4 5 6 7
|
Date::Date()
: month(1),
day(1),
year(1),
monthName("January")
{
}
|
But for this to work you need to actually declare monthName as a member variable (maybe you want to consider if you really need both
month and
monthName).
There are also some warnings about "multi-character character constant" comming from
'10'
,
'11'
and
'12'
. Note that single quotes are normally used for characters (char). Putting multiple characters between single quotes means something different but it's still not what you want.
'1'
is not equal to
1
. So just remove the single quotes around the numbers!
___
http://cplusplus.com/forum/general/283271/#msg1226232 ___
The third post:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include "Date.h"
#include <iostream>
using namespace CurrentDate; // Where is CurrentDate declared?
using namespace std;
int main()
{
Date::Date(); // This is not how you create a variable (object).
cout << "Hello world!" << endl;
PrintNumDate(int month, int day, int year); // This is not how you call a member function.
return 0;
}
|
The code should probably look more like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include "Date.h"
#include <iostream>
using namespace std;
int main()
{
Date date(4, 22, 2022); // Assuming you have a Date constructor that takes three ints as argument.
cout << "Hello world!" << endl;
date.PrintNumDate(); // For this to work you would need to change the Date::PrintNumDate() member
// function so that it doesn't take any arguments and instead prints the day,
// month and year of the date object that it's called on.
return 0;
}
|