C ++无法为子类编写构造函数

时间:2015-03-30 09:17:46

标签: c++ class compiler-errors

问题解决了!谢谢你的帮助。

我正在为我的程序编写调度程序。我有两个课程:task_timepointtask_periodtask_periodtask_timepoint的派生类。

我为task_timepoint编写了构造函数,但是当我开始为task_period编写构造函数时,我遇到了一些我无法解决的错误。

scheduler.hpp

#ifndef SCHEDULER_H
#define SCHEDULER_H

#include "boost/date_time/posix_time/posix_time.hpp"
#include <functional>
#include <chrono>

namespace schelduler{
    using namespace schelduler;
    using namespace boost::posix_time;

    class task_timepoint{
        protected:
            ptime run_time; //Time at(after) that task_function should run
            std::function<void(void)> task_function; //Function that should be run at specified time

        public:
            task_timepoint(ptime run_time, std::function<void(void)> task_function);
    };

    class task_period : public task_timepoint{
        private:
            time_duration run_period;

        public:
            task_period(time_duration run_period, std::function<void(void)> task_function);
    };
}
 #endif

scheduler.cpp

#include "scheduler.hpp"

using namespace schelduler;
using namespace boost::posix_time;

task_timepoint::task_timepoint(ptime run_time, std::function<void(void)> task_function)
{
    task_timepoint::run_time = run_time;
    task_timepoint::task_function = task_function;
}

task_period::task_period(time_duration run_period, std::function<void(void)> task_function)
{
    this->run_period = run_period;
    //task_period::task_function = task_function;
}

错误:

/home/dm3ch/Workspace/Refregiration_Telemetry/Device/src/scheduler.cpp: In constructor ‘schelduler::task_period::task_period(boost::posix_time::time_duration, std::function<void()>)’:
/home/dm3ch/Workspace/Refregiration_Telemetry/Device/src/scheduler.cpp:12:91: error: no matching function for call to ‘schelduler::task_timepoint::task_timepoint()’
 task_period::task_period(time_duration run_period, std::function<void(void)> task_function)
                                                                                           ^
/home/dm3ch/Workspace/Refregiration_Telemetry/Device/src/scheduler.cpp:12:91: note: candidates are:
/home/dm3ch/Workspace/Refregiration_Telemetry/Device/src/scheduler.cpp:6:1: note: schelduler::task_timepoint::task_timepoint(boost::posix_time::ptime, std::function<void()>)
 task_timepoint::task_timepoint(ptime run_time, std::function<void(void)> task_function)
 ^
/home/dm3ch/Workspace/Refregiration_Telemetry/Device/src/scheduler.cpp:6:1: note:   candidate expects 2 arguments, 0 provided
In file included from /home/dm3ch/Workspace/Refregiration_Telemetry/Device/src/scheduler.cpp:1:0:
/home/dm3ch/Workspace/Refregiration_Telemetry/Device/include/scheduler.hpp:12:11: note: schelduler::task_timepoint::task_timepoint(const schelduler::task_timepoint&)
     class task_timepoint{
           ^
/home/dm3ch/Workspace/Refregiration_Telemetry/Device/include/scheduler.hpp:12:11: note:   candidate expects 1 argument, 0 provided
/home/dm3ch/Workspace/Refregiration_Telemetry/Device/include/scheduler.hpp:12:11: note: schelduler::task_timepoint::task_timepoint(schelduler::task_timepoint&&)
/home/dm3ch/Workspace/Refregiration_Telemetry/Device/include/scheduler.hpp:12:11: note:   candidate expects 1 argument, 0 provided
make[2]: *** [CMakeFiles/Refregiration_Telemetry-Device.dir/src/scheduler.cpp.o] Error 1
make[1]: *** [CMakeFiles/Refregiration_Telemetry-Device.dir/all] Error 2
make: *** [all] Error 2

抱歉我的英文不好

1 个答案:

答案 0 :(得分:2)

您的task_period构造函数尝试调用task_timepoint基类的默认构造函数。你需要像这样明确地调用它:

task_period::task_period(time_duration run_period, std::function<void(void)> task_function)
    : task_timepoint{run_period, task_function},
      run_period{run_period}
{}