Инициализировать карту с лямбда-функциями в качестве значенийC++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Инициализировать карту с лямбда-функциями в качестве значений

Сообщение Anonymous »

Я пытаюсь инициализировать карту в функции-конструкторе фабричного класса. Карта имеет строки в качестве ключей и лямбда-функции в качестве значений. Лямбда-функции сами по себе являются конструкторами других классов, наследуемых от интерфейса «IComponent». Этот фрагмент кода воспроизводит ошибку, которую я получаю:
#include
#include
#include
#include

namespace Mynamespace
{
class IComponent {
virtual ~IComponent() = default;
};
}

namespace Mynamespace
{
class Input {
public:
Input();
~Input();
};
}

namespace Mynamespace
{
class Output {
public:
Output();
~Output();
};
}

namespace Mynamespace
{
class Factory {
private:
std::map Map;
public:
Factory();
void initializeMap();
}

void Mynamespace::Factory::initializeMap()
{
Map = {
{"input", [](){return std::make_unique();}},
{"output", [](){return std::make_unique();}}
};
}

сообщение об ошибке:
test.cpp:39:2: error: expected ‘;’ after class definition
39 | }
| ^
| ;
test.cpp: In member function ‘void Mynamespace::Factory::initializeMap()’:
test.cpp:46:5: error: no match for ‘operator=’ (operand types are ‘std::map’ and ‘
’)
46 | };
| ^
In file included from /usr/include/c++/13/map:63,
from test.cpp:4:
/usr/include/c++/13/bits/stl_map.h:331:7: note: candidate: ‘std::map& std::map::operator=(const std::map&) [with _Key = const std::__cxx11::basic_string; _Tp = std::function; _Compare = std::less; _Alloc = std::allocator]’
331 | operator=(const map&) = default;
| ^~~~~~~~
/usr/include/c++/13/bits/stl_map.h:331:17: note: no known conversion for argument 1 from ‘
’ to ‘const std::map&’
331 | operator=(const map&) = default;
| ^~~~~~~~~~
/usr/include/c++/13/bits/stl_map.h:335:7: note: candidate: ‘std::map& std::map::operator=(std::map&&) [with _Key = const std::__cxx11::basic_string; _Tp = std::function; _Compare = std::less; _Alloc = std::allocator]’
335 | operator=(map&&) = default;
| ^~~~~~~~
/usr/include/c++/13/bits/stl_map.h:335:17: note: no known conversion for argument 1 from ‘
’ to ‘std::map&&’
335 | operator=(map&&) = default;
| ^~~~~
/usr/include/c++/13/bits/stl_map.h:349:7: note: candidate: ‘std::map& std::map::operator=(std::initializer_list) [with _Key = const std::__cxx11::basic_string; _Tp = std::function; _Compare = std::less; _Alloc = std::allocator]’
349 | operator=(initializer_list __l)
| ^~~~~~~~
/usr/include/c++/13/bits/stl_map.h:349:46: note: no known conversion for argument 1 from ‘
’ to ‘std::initializer_list’
349 | operator=(initializer_list __l)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
test.cpp: At global scope:
test.cpp:47:2: error: expected ‘}’ at end of input
47 | }
| ^
test.cpp:32:1: note: to match this ‘{’
32 | {


Подробнее здесь: https://stackoverflow.com/questions/798 ... -as-values
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»