c++ - C++不能为子类编写构造函数

标签 c++ class compiler-errors

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

我正在为程序编写调度程序。我有2类: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

对不起,我的英语不好

最佳答案

您的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}
{}

关于c++ - C++不能为子类编写构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29342436/

相关文章:

java - 有什么方法可以覆盖 Java 应用程序中的库类

c++ - 在 C 中使用无效指针值是否合法?

c++ - C++中的简单矩阵求幂

c++ - 多线程应用程序上的 volatile C/C++

java - 无法在另一个类中创建打印方法并在主类中调用它

javascript - 超出最大调用堆栈大小 - 无限循环

c++ - 错误 : no matching function for call to

java - 如何解决无法在Java oop中找到或加载主类?

c - C 和 dlc 编译器出现奇怪的解析错误

generics - Java 8通配符类型不能直接实例化