I created the ServiceCommand
class, from which I inherited UndoCommand
and RedoCommand
. Now I'm writing a try_create_service_command
function that should return a pointer to the ServiceCommand
. To do this, I created a variable std :: unique_ptr <ServiceCommand> cmd = nullptr
in the function, and when parsing dto-objects, I do cmd = std ::make_unique<UndoCommand>()
.
try_create_service_command()
implementation :
std::optional<std::unique_ptr<ServiceCommand>> CommandCreator::try_create_service_command() {
if (dto_objects.empty()) {
return {};
}
std::unique_ptr<ServiceCommand> cmd = nullptr;
auto dto = dto_objects.front();
auto name = dto.get_name();
switch (name) {
case Command::UNDO:
cmd = std::make_unique<UndoCommand>();
dto_objects.erase(dto_objects.begin());
break;
case Command::REDO:
cmd = std::make_unique<RedoCommand>();
dto_objects.erase(dto_objects.begin());
break;
default:
return {};
}
return cmd;
}
At lines cmd = std::make_unique<UndoCommand>();
and cmd = std::make_unique<RedoCommand>();
the compiler says:
[ 23%] Building CXX object CMakeFiles/text_editor.dir/command_creator.cpp.o
[ 23%] Building CXX object CMakeFiles/text_editor.dir/undo.cpp.o
In file included from /Users/dilshod/CLionProjects/text_editor/redo.cpp:1:
In file included from /Users/dilshod/CLionProjects/text_editor/redo.h:3:
In file included from /Users/dilshod/CLionProjects/text_editor/service_command.h:3:
In file included from /Users/dilshod/CLionProjects/text_editor/editor.h:4:
/Users/dilshod/CLionProjects/text_editor/undo.h:4:34: error: expected class name
class UndoCommand final : public ServiceCommand{
^
/Users/dilshod/CLionProjects/text_editor/undo.h:6:18: error: unknown type name 'Editor'
void execute(Editor& editor) const override;
^
In file included from /Users/dilshod/CLionProjects/text_editor/undo.cpp:1:
In file included from /Users/dilshod/CLionProjects/text_editor/undo.h:2:
In file included from /Users/dilshod/CLionProjects/text_editor/service_command.h:3:
In file included from /Users/dilshod/CLionProjects/text_editor/editor.h:5:
/Users/dilshod/CLionProjects/text_editor/redo.h:6:34: error: expected class name
class RedoCommand final : public ServiceCommand{
^
/Users/dilshod/CLionProjects/text_editor/redo.h:8:18: error: unknown type name 'Editor'
void execute(Editor& editor) const override;
^
22 errors generated.
errors generated.
make[3]: *** [CMakeFiles/text_editor.dir/undo.cpp.o] Error 1
make[3]: *** Waiting for unfinished jobs....
make[3]: *** [CMakeFiles/text_editor.dir/redo.cpp.o] Error 1
In file included from /Users/dilshod/CLionProjects/text_editor/command_creator.cpp:1:
In file included from /Users/dilshod/CLionProjects/text_editor/command_creator.h:12:
In file included from /Users/dilshod/CLionProjects/text_editor/service_command.h:3:
In file included from /Users/dilshod/CLionProjects/text_editor/editor.h:4:
/Users/dilshod/CLionProjects/text_editor/undo.h:4:34: error: expected class name
class UndoCommand final : public ServiceCommand{
^
/Users/dilshod/CLionProjects/text_editor/undo.h:6:18: error: unknown type name 'Editor'
void execute(Editor& editor) const override;
^
In file included from /Users/dilshod/CLionProjects/text_editor/command_creator.cpp:1:
In file included from /Users/dilshod/CLionProjects/text_editor/command_creator.h:12:
In file included from /Users/dilshod/CLionProjects/text_editor/service_command.h:3:
In file included from /Users/dilshod/CLionProjects/text_editor/editor.h:5:
/Users/dilshod/CLionProjects/text_editor/redo.h:6:34: error: expected class name
class RedoCommand final : public ServiceCommand{
^
/Users/dilshod/CLionProjects/text_editor/redo.h:8:18: error: unknown type name 'Editor'
void execute(Editor& editor) const override;
^
/Users/dilshod/CLionProjects/text_editor/command_creator.cpp:48:17: error: no viable overloaded '='
cmd = std::make_unique<UndoCommand>();
~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2426:28: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'unique_ptr<UndoCommand, default_delete<UndoCommand>>' to 'const unique_ptr<ServiceCommand, default_delete<ServiceCommand>>' for 1st argument
class _LIBCPP_TEMPLATE_VIS unique_ptr {
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2541:15: note: candidate function not viable: no known conversion from 'unique_ptr<UndoCommand, default_delete<UndoCommand>>' to 'unique_ptr<ServiceCommand, default_delete<ServiceCommand>>' for 1st argument
unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2580:15: note: candidate function not viable: no known conversion from 'typename __unique_if<UndoCommand>::__unique_single' (aka 'unique_ptr<UndoCommand>') to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument
unique_ptr& operator=(nullptr_t) _NOEXCEPT {
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2552:15: note: candidate template ignored: requirement 'is_convertible<UndoCommand *, ServiceCommand *>::value' was not satisfied [with _Up = UndoCommand, _Ep = std::__1::default_delete<UndoCommand>]
unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
^
/Users/dilshod/CLionProjects/text_editor/command_creator.cpp:55:17: error: no viable overloaded '='
cmd = std::make_unique<RedoCommand>();
~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2426:28: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'unique_ptr<RedoCommand, default_delete<RedoCommand>>' to 'const unique_ptr<ServiceCommand, default_delete<ServiceCommand>>' for 1st argument
class _LIBCPP_TEMPLATE_VIS unique_ptr {
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2541:15: note: candidate function not viable: no known conversion from 'unique_ptr<RedoCommand, default_delete<RedoCommand>>' to 'unique_ptr<ServiceCommand, default_delete<ServiceCommand>>' for 1st argument
unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2580:15: note: candidate function not viable: no known conversion from 'typename __unique_if<RedoCommand>::__unique_single' (aka 'unique_ptr<RedoCommand>') to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument
unique_ptr& operator=(nullptr_t) _NOEXCEPT {
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2552:15: note: candidate template ignored: requirement 'is_convertible<RedoCommand *, ServiceCommand *>::value' was not satisfied [with _Up = RedoCommand, _Ep = std::__1::default_delete<RedoCommand>]
unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
^
6 errors generated.
make[3]: *** [CMakeFiles/text_editor.dir/command_creator.cpp.o] Error 1
make[2]: *** [CMakeFiles/text_editor.dir/all] Error 2
make[1]: *** [CMakeFiles/text_editor.dir/rule] Error 2
make: *** [text_editor] Error 2
Classes:
#include "editor.h"
class Editor;
class ServiceCommand
{
public:
ServiceCommand() = default;
virtual ~ServiceCommand() = default;
virtual void execute(Editor& editor) const = 0;
};
class RedoCommand final : public ServiceCommand{
public:
void execute(Editor& editor) const override;
};
class UndoCommand final : public ServiceCommand{
public:
void execute(Editor& editor) const override;
};
How to fix ?