no matching function for call to ""

Pages: 12
This is my specification file, and my implementation file isnt working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef DATE_H
#define DATE_H
#include <string>
class Date {
	private:
	int monthDefault;
	int dayDefault;
	int yearDefault;
	std::string monthName;
	public:
	
	~Date(){}
    int dayNumber(int);
	void monthWord(int);
	int yearNumber(int);
	void showDates(int, int, int);
};
#endif 

Here is my implementation file, and its not working.
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "Date.h"
#include <string>
#include <iostream>

int Date::dayNumber(int date)
{
	int dayDefault = 1;
	if (date < 1 || date > 31)
	{
		date = dayDefault;
		return date;
	}
	else
	{
		return date;
	}
}

void Date::monthWord(int month)
{
	int monthDefault = 1;
	if (month < 1 || month > 12)
	{
		month = monthDefault;
		monthName = "January";
	}
	switch (month)
	{
		case 1:
		monthName = "January";
		break;
		case 2:
		monthName = "February";	
		break;
		case 3:
		monthName = "March";
		break;
		case 4:
		monthName = "April";
		break;
		case 5:
		monthName = "May";
		break;
		case 6:
		monthName = "June";
		break;
		case 7:
		monthName = "July";
		break;
		case 8:
		monthName = "August";
		break;
		case 9:
		monthName = "September";
		break;
		case 10:
		monthName = "October";
		break;
		case 11:
		monthName = "November";
		break;
		case 12:
		monthName = "December";
		break;
	}
}

int Date::yearNumber(int year)
{
	int yearDefault = 2000;
	if (year < 1900 || year > 2099)
	{
		year = yearDefault;
		return year;
	}
	return year;
}

void Date::showDates(int date, int month, int year)
{
	std::cout << month << "/" << date << "/" << year << std::endl;
}

int main()
{
	Date dates(1, 1, 2000);
	dates.showDates();
}
I know the main is supposed to be in another file, but if i do that it tells me some thing about winmain error, and im just testing it in the implementation file.
You’ve got a destructor but no constructor
I know the main is supposed to be in another file, but if i do that it tells me some thing about winmain error,


This probably means that you selected the wrong type of program when you created your project. To use main() you should probably select a console program, not a Win32 program.

By the way it would help if you actually told us what is wrong with your code. If you're getting error messages post the complete messages, all of them exactly as they appear in your development environment.


You're missing a constructor. Also, in your main, dates.showDates requires 3 inputs, while you aren't giving any.

You already have a destructor, ~Date(){}, however you don't have a constructor. This would be your constructor Date(){}. The constructor doesn't have the ~.

When you create your object:
Date dates(1, 1, 2000);, the (1, 1, 2000) part of it is the constructor, which takes in arguments when you create an object.

An example for your constructor would be this:

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
//this is in Date.h

#ifndef DATE_H
#define DATE_H
#include <string>
class Date {

	public:
        
        //this is your constructor here
	Date(int day, int month, int year);

};
#endif 



//this is Date.cpp

#include "Date.h"
#include <string>
#include <iostream>

//define what your constructor does
Date::Date(int day, int month, int year) {

        //do whatever you want your constructor to do here

}

int main() {

        //create your object
        Date dates(1, 1, 2000); 

        //you were missing arguments here in your showDates call. 
        //did you mean for this to have no inputs, or did your forget to add any?
        dates.showDates(2, 24, 2010);

}
It works now!

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#ifndef DATE_H
#define DATE_H

#include <string>

class Date {
    private:
    int monthDefault; // <-- Default part is not a good name and not what u mean
    int dayDefault;
    int yearDefault;
    std::string monthName;
    public:
    Date(int month, int day, int year) // <--
    {
        monthDefault = month;
        dayDefault = day;
        yearDefault = year;
    }
    
    ~Date(){}
    int dayNumber(int);
    void monthWord(int);
    int yearNumber(int);
    void showDates(); // <--
};
#endif

//#include "Date.h"
#include <string>
#include <iostream>

int Date::dayNumber(int date)
{
    int dayDefault = 1;
    if (date < 1 || date > 31)
    {
        date = dayDefault;
        return date;
    }
    else
    {
        return date;
    }
}

void Date::monthWord(int month)
{
    int monthDefault = 1;
    if (month < 1 || month > 12)
    {
        month = monthDefault;
        monthName = "January";
    }
    switch (month)
    {
        case 1:
        monthName = "January";
        break;
        case 2:
        monthName = "February";
        break;
        case 3:
        monthName = "March";
        break;
        case 4:
        monthName = "April";
        break;
        case 5:
        monthName = "May";
        break;
        case 6:
        monthName = "June";
        break;
        case 7:
        monthName = "July";
        break;
        case 8:
        monthName = "August";
        break;
        case 9:
        monthName = "September";
        break;
        case 10:
        monthName = "October";
        break;
        case 11:
        monthName = "November";
        break;
        case 12:
        monthName = "December";
        break;
    }
}

int Date::yearNumber(int year)
{
    int yearDefault = 2000;
    if (year < 1900 || year > 2099)
    {
        year = yearDefault;
        return year;
    }
    return year;
}

void Date::showDates() // <--
{
    std::cout << monthDefault << "/" << dayDefault << "/" << yearDefault << std::endl;
}

int main()
{
    Date dates(7, 1, 2000);
    dates.showDates();
}
PS the switch structure is workable but another way is to have an array of
std::string month_names[]= {'dummy", "January", "February", ...}, so you end up with the month name for a particular date =month_name[date.monthDefault]
Ok I modified mine to make it work like you made it, but the words for the months do not display when i put for example
 
std::cout << monthName << " " << dayDefault << ", " << yearDefault << std::endl;

in the showDates
Might be a good idea to post your latest code.
i mean its your code but with one part in the .h file, and the other in a .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
#ifndef DATE_H
#define DATE_H

#include <string>

class Date {
    private:
    int dayDefault;
    int yearDefault;
    std::string monthName;
    int monthDefault = 1;
    public:
    	
    Date(int month, int day, int year) // <--
    {
        monthDefault = month;
        dayDefault = day;
        yearDefault = year;
    }
    
    ~Date(){}
    int dayNumber(int);
    void monthWord(int, int);
    int yearNumber(int);
    void showDates(); // <--
};
#endif 


.h file
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "Date.h"
#include <string>
#include <iostream>
#include <iomanip>
int Date::dayNumber(int date)
{
    int dayDefault = 1;
    if (date < 1 || date > 31)
    {
        date = dayDefault;
        return date;
    }
    else
    {
        return date;
    }
}

void Date::monthWord(int month, int date)
{
	dayDefault = 1;
    if (month < 1 || month > 12)
    {
        month = monthDefault;
        monthName = "January";
    }
    switch (month)
    {
        case 1:
        monthName = "January";
        if (date < 1 || date > 31)
    	{
        	date = dayDefault;
    	}
        break;
        case 2:
        monthName = "February";
        if (date < 1 || date > 29)
    	{
        	date = dayDefault;
   	 	}
        break;
        case 3:
        monthName = "March";
        if (date < 1 || date > 31)
    	{
        	date = dayDefault;
    	}
        break;
        case 4:
        monthName = "April";
        if (date < 1 || date > 30)
    	{
        	date = dayDefault;
    	}
        break;
        case 5:
        monthName = "May";
        if (date < 1 || date > 31)
    	{
        	date = dayDefault;
    	}
        break;
        case 6:
        monthName = "June";
        if (date < 1 || date > 30)
    	{
        	date = dayDefault;
    	}
        break;
        case 7:
        monthName = "July";
        if (date < 1 || date > 31)
    	{
        	date = dayDefault;
    	}
        break;
        case 8:
        monthName = "August";
        if (date < 1 || date > 31)
    	{
        	date = dayDefault;
    	}
        break;
        case 9:
        monthName = "September";
        if (date < 1 || date > 30)
    	{
        	date = dayDefault;
    	}
        break;
        case 10:
        monthName = "October";
        if (date < 1 || date > 31)
    	{
        	date = dayDefault;
    	}
        break;
        case 11:
        monthName = "November";
        if (date < 1 || date > 31)
    	{
        	date = dayDefault;
    	}
        break;
        case 12:
        monthName = "December";
        if (date < 1 || date > 31)
    	{
        	date = dayDefault;
    	}
        break;
    }
}

int Date::yearNumber(int year)
{
    int yearDefault = 2000;
    if (year < 1900 || year > 2099)
    {
        year = yearDefault;
        return year;
    }
    return year;
}

void Date::showDates() // <--
{
    std::cout << monthDefault << "/" << dayDefault << "/" << yearDefault << std::endl;
    std::cout << monthName << " " << dayDefault << ", " << yearDefault << std::endl;
    std::cout << dayDefault << "." << monthName << "." << yearDefault << std::endl;
    std::cout << std::setprecision(2) << yearDefault << "." << std::setfill('0') << std::setw(2) << monthDefault << "." << std::setfill('0') << std::setw(2) << dayDefault << std::endl;
}

int main()
{
    Date dates(4, 31, 2021);
    dates.showDates();
}

.cpp file
Nothing will show for the monthName until you call the monthWord() method.

Given the way you’re going about it you can do it in the constructor.
Wait how exactly do I call it? I thought I called it already.
Last edited on
Maybe my blind, so where/which file/ which line number
line 23 on the first file?
You have a couple of "shadowed" warnings you need to fix.

ie:
1
2
3
int Date::yearNumber(int year)
{
    int yearDefault = 2000;

Do you realize that that second line is creating another int variable named yearDefault that is local to that function? Any changes to that variable doesn't alter the class variable named yearDefault.

Also you don't initialize the month name in your constructor and you never call the monthWord function so the name will never have a value.

line 23 on the first file?


Yeah, well leaving that where it is, to fix what’s going on you need to call the function and a convenient place to do it is in the constructor.

And what that means is a new line like this

1
2
3
4
5
6
7
Date(int month, int day, int year) // <--
    {
        monthDefault = month;
        dayDefault = day;
        yearDefault = year;
        monthWord(); // <—
    }
[Error] expected primary-expression before 'int'
I added monthWord();
to be exact,
1
2
        yearDefault = year;
        monthWord(month, date);

and it returns after compiling
Last edited on
Pages: 12