简体   繁体   中英

C++ pure virtual error

I am trying to inherit from an abstract class but getting the following errors:

In file included from /usr/include/c++/5/vector:64:0, from /usr/include/boost/format.hpp:17, from /usr/include/boost/math/policies/error_handling.hpp:31, from /usr/include/boost/math/special_functions/round.hpp:14, from /opt/ros/kinetic/include/ros/time.h:58, from /opt/ros/kinetic/include/ros/ros.h:38, from /home/gil/catkin_ws/src/manager/include/Tasks/Task.h:4, from /home/gil/catkin_ws/src/manager/src/Tasks/Task.cpp:1: /usr/include/c++/5/bits/stl_vector.h:713:7: error: invalid abstract parameter type 'manager::Task' resize(size_type __new_size, value_type __x = value_type())

--

/home/gil/catkin_ws/src/manager/include/Tasks/Task.h:11:16: note:
virtual void manager::Task::Execute() virtual void Execute() = 0; ^ In file included from /usr/include/c++/5/vector:64:0, from /usr/include/boost/format.hpp:17, from /usr/include/boost/math/policies/error_handling.hpp:31, from /usr/include/boost/math/special_functions/round.hpp:14, from /opt/ros/kinetic/include/ros/time.h:58, from /opt/ros/kinetic/include/ros/ros.h:38, from /home/gil/catkin_ws/src/manager/include/Tasks/Task.h:4, from /home/gil/catkin_ws/src/manager/include/Tasks/RootTask.h:10, from /home/gil/catkin_ws/src/manager/src/Tasks/RootTask.cpp:8: /usr/include/c++/5/bits/stl_vector.h:713:7: error: invalid abstract parameter type 'manager::Task' resize(size_type __new_size, value_type __x = value_type())

--

manager/CMakeFiles/manager.dir/build.make:110: recipe for target 'manager/CMakeFiles/manager.dir/src/Tasks/Task.cpp.o' failed make[2]: * [manager/CMakeFiles/manager.dir/src/Tasks/Task.cpp.o] Error 1 make[2]: * Waiting for unfinished jobs.... manager/CMakeFiles/manager.dir/build.make:158: recipe for target 'manager/CMakeFiles/manager.dir/src/Tasks/RootTask.cpp.o' failed make[2]: *** [manager/CMakeFiles/manager.dir/src/Tasks/RootTask.cpp.o] Error 1 In file included from /usr/include/c++/5/vector:64:0, from /usr/include/boost/format.hpp:17, from /usr/include/boost/math/policies/error_handling.hpp:31, from /usr/include/boost/math/special_functions/round.hpp:14, from /opt/ros/kinetic/include/ros/time.h:58, from /opt/ros/kinetic/include/ros/ros.h:38, from /home/gil/catkin_ws/src/manager/src/indoor_mission_action_server.cpp:9: /usr/include/c++/5/bits/stl_vector.h:713:7: error: invalid abstract parameter type 'manager::Task' resize(size_type __new_size, value_type __x = value_type())

Not sure what am i missing... Here are my H and CPP files:

RootTask.cpp

#include "Tasks/RootTask.h"
namespace manager {
  RootTask::RootTask(std::string name) {
    mTaskName = name;
  }
  RootTask::~RootTask() {}
  RootTask::RootTask() {}

  void RootTask::Execute() {}

  void RootTask::setTaskSeqByName() {}
  std::string RootTask::GetTaskName() {
    return mTaskName;
  }
}

RootTask.h

#include "Tasks/Task.h"
namespace manager {
  class RootTask: public Task {
    public:

      RootTask();
    RootTask(std::string name);~RootTask();

    void Execute();
    void setTaskSeqByName();
    std::string GetTaskName();
  };
}

Task.cpp

#include "Tasks/Task.h"
#include < iostream >

  namespace manager {
    void Task::Stop() {}
    void Task::Pause() {}
    void Task::OnError() {}
    std::string Task::GetTaskName() {}
    void Task::setTaskSeqByName() {}
  }

Task.h

#pragma once

#include < iostream > 
#include "ros/ros.h"

namespace manager {
  class Task {
    public:
      virtual void Execute() = 0;
    virtual void setTaskSeqByName();
    virtual void Stop();
    virtual void Pause();
    virtual void OnError();
    virtual std::string GetTaskName();

    protected:
      std::string mTaskName;
    std::vector < Task > mTaskSequence;
  };
}

Thank you for the assistance.

In Task you have a member

std::vector < Task > mTaskSequence;

You cannot have this as Task is an abstract class and you cannot make an instance of it. You need to use a pointer type like

std::vector<std::unique_ptr<Task>> mTaskSequence;

The answer by @NathanOliver points out how you can fix the compiler error.

I want to point out that having a Task contain list of pointers to other Task s seems to be a possible design flaw.

You may want to consider moving it up to a higher level class that deals with sequences of Task s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM