【问题标题】:C++ can't write constructor for child classC++ 不能为子类编写构造函数
【发布时间】:2015-03-30 09:17:46
【问题描述】:

问题解决了!感谢您的帮助。

我正在为我的程序编写调度程序。我有 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_period 的派生类。" !!??!?!
  • 编辑您的问题并在此处发布相关代码和错误。
  • 我根本不会关注任何指向小示例的外部链接!请在此处发布您的代码,并告诉我们您收到了哪些错误消息。

标签: c++ class compiler-errors


【解决方案1】:

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

【讨论】:

  • 但是,如果我不能为 task_timepoint 构造函数设置所有参数,我该怎么办?
  • 如果您无法为task_timepoint 设置所有参数,您需要考虑哪些数据是类的固有数据,哪些数据是附带的。 task_timepoint 可以在没有这些信息的情况下存在吗?如果是这样,请创建另一个构造函数,该构造函数采用您绝对需要的子集。如果没有,可能您没有在合适的时间创建 task_timepoint 对象。
猜你喜欢
  • 2014-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
相关资源
最近更新 更多