No matching function

Since I'm doing this for a problem of sorts on a site anyone can join, I won't release my whole code, but here's what I tried.

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
#include <iostream>

enum month {jan,feb,mar,apr,jun,jul,aug,sep,oct,nov,dec};
enum weekday {sun,mon,tue,wed,thur,fri,sat};

int year;
int numberofdays(month);
void plusone7(weekday&);

int main()
{
    int number = 0;
    int suns = 0;
    //Some code
    std::cout << number << " days total." << std::endl;
    weekday dayofweek;
    for (int day = 1,dayofweek = mon;day<=number;day++,plusone7(dayofweek)) //"No matching function for call to plusone7"
    {
        if (dayofweek == sun)
        {
            suns++;
        }
    }
    std::cout << suns << " is the number of sundays" << std::endl;
}

//Defines a few functions

void plusone7(weekday& added)
{
    if (added == sat)
    {
        added = sun;
    }
    else
    {
        added++;
    }
}


But the prototype and the bit at the end would make there a matching function, I thought?
I'm no expert at C++, but try removing the pointer on weekday on Line 8.
No, that's a reference, it gets passed the variable itself instead of a copy of it, basically, so the function can modify its argument.
Ah. Are you sure void is what you want the type to be? I'm basically just attacking line 8 since the compiler thinks that plusone7 is not declared.
It doesn't need to return anything anywhere.
One thing. What's the difference between dayofweek and weekday?
weekday is a type, dayofweek is a variable (object, technically) of type weekday.
How can weekday be a type when it has a type?
enum weekday {/*some stuff*/}; declares a new type called weekday with literals sun,mon,tue,etc.
Last edited on
gcc wrote:
error: invalid initialization of reference of type ‘weekday&’ from expression of type ‘int’

What line is that error on? My guess is it's on line 37 on your compiler.
Last edited on
Nope, line 17.
for (int day = 1,dayofweek = mon;//... here you are making an int variable called 'dayofweek'.
As it is an inner scope there is no name collision, so you've got both 'dayofweek', the integer and the day. But the compiler thinks that you want to use the integer one (as that's from the inner scope).

1
2
    weekday dayofweek = mon;
    for (int day = 1;//... 


Oh, and yes, line 37 is illegal.
Lin 37 is illegal? If my reasoning at closer inspection is correct, I'd have to do some casting to int (or maybe int&) and back, correct?

Anyway, how am I declaring an int dayofweek there? I am declaring an int day, which is different, and I am initializing the weekday dayoftheweek. Sorry if I'm sounding a bit bitter, or just missing something incredibly obvious and refusing to see it, neither are my intention.
Last edited on
You can declare several variables of the same type in one line. Simply put their names separated by commas.
By instance:
1
2
//type name1, name2, ...;
int number = 0, suns = 0; //declaring two integers 


That's what you wrote on line 17, and that's what the compiler understood, declare two numbers.
Oh. *Facepalm* Thanks. I seem to make incredibly stupid mistakes such as this a lot.
Topic archived. No new replies allowed.