关于c 11:C纯虚错误

C++ pure virtual error

我正在尝试从抽象类继承,但出现以下错误:

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 a€?manager::Taska€?
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 a€?manager::Taska€?
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 a€?manager::Taska€?
resize(size_type __new_size, value_type __x = value_type())

不知道我错过了什么......
这是我的 H 和 CPP 文件:

RootTask.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
#include"Tasks/Task.h"
namespace manager {
  class RootTask: public Task {
    public:

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

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

任务.cpp

1
2
3
4
5
6
7
8
9
10
#include"Tasks/Task.h"
#include < iostream >

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

任务.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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;
  };
}

感谢您的帮助。


Task 你有一个成员

1
std::vector < Task > mTaskSequence;

你不能拥有它,因为 Task 是一个抽象类,你不能创建它的实例。您需要使用像

这样的指针类型

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

@NathanOliver 的回答指出了如何修复编译器错误。

我想指出,让 Task 包含指向其他 Task 的指针列表似乎是一个可能的设计缺陷。

您可能需要考虑将其移至处理 Tasks 序列的更高级别的类。