No matching function for call, annoying C++ error

时间:2016-02-12 19:21:35

标签: c++

So I'm very new to C++ and I'm trying to do some shenanigans with classes, but I'm getting a very annoying error I don't know how to fix.

#include "clock.h"
#include <iostream>
#include <tuple>

using namespace std;

int secs;
int mins;
int hours;

clock::clock(int secs, int mins, int hours){
    secs = secs;
    mins = mins;
    hours = hours;
}

void clock::tick(){
}

void clock::print(){
    cout << "The time is: " << hours << ":" << mins << ":" << secs << endl;
}

and then here's my second class

#include <iostream>
#include "normalclock.h"
#include "clock.h"
#include <tuple>

using namespace std;

    int secs;
    int mins;
    int hours;

    NormalClock::NormalClock(int secs, int mins, int hours){
        secs = secs;
        mins = mins;
        hours = hours;
    }

    void NormalClock::tick(){
        secs ++;
        if(secs == 60){
            mins++;
            secs = 0;
        }
        if(mins == 60){
           hours++;
           mins = 0;
        }
        if(hours == 24) {
            hours = 0;
        }
            }

The error I'm getting says

error: no matching function for call to 'clock::clock()'
     NormalClock::NormalClock(int secs, int mins, int hours){
                                                           ^

What does one do in this situation?

2 个答案:

答案 0 :(得分:0)

You have these global variables that don't belong to your class:

int secs; // what is this for? this should be in clock's definition
int mins;
int hours;

Next, you attempt to set them inside constructors, but you end up assigning parameters to themselves:

secs = secs;
mins = mins;
hours = hours;

Finally, you didn't provide the definitions of the classes, but it looks like NormalClock inherits clock, which does not have a default constructor (because you've provided your own), which is requested before entering the NormalClock constructor's body.

It should be like this:

// no variable declarations here

NormalClock::NormalClock(int secs, int mins, int hours) :
clock(secs, mins, hours)
{
    // no assignments here, this class inherits all members from clock
}

Learn about member initializer lists and probably much more about classes and scopes.

答案 1 :(得分:0)

What does one do in this situation?

This question doesn't make sense. Developing software is not something that you stumble upon, you write code to execute precise things, it's not that you just find it on ground.

There is a clock class, there is a NormalClock which presumably (since you are not providing declarations) inherits from clock.

Now you define a constructor for NormalClock which takes 3 arguments but you don't specify how the superclass clock should be constructed. The compiler is trying for you to use a default clock::clock() constructor but clock has an user defined constructor, so the default one is not generated unless you explicit that.

You should call the super constructor, eg

NormalClock::NormalClock(...) : clock(...) {

}

or provide clock with a default constructor, eg

class clock {
public:
  clock() = default;
};

but this makes little sense since you are passing 3 arguments to NormalClock constructor which, still presumably, should be forwarded to the superclass constructor.