error: unitialized member with const type c++
error: unitialized member with const type c++
我正在编写一个程序来传递我不想修改的结构类型。该结构有两个 const
成员,如下所示:
struct system_s {
std::string name;
std::string pkg;
char *const start_cmd[10];
char *const end_cmd[10];
bool ros;
bool equals(const system_s &cmp);
};
结构存储在具有以下格式的地图中。它是 class 成员:
std::map<std::string, system_s> sys_map;
还有一张临时地图。如果您愿意,可以将 sys_map
视为缓存。但实际上你不必担心它是如何被用于这个问题的。正在调用 sys_map
以将系统添加到临时地图,如下所示。它在 class 方法中:
add_system(sys_map[msg->system]);
(*)
这个函数有如下定义。这是一个class方法:
int add_system(const system_s &sys);
调用 (*) 时,出现以下错误:
system.h: In instantiation of ?std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::basic_string<char>; _Tp = system_s; _Compare = std::less<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, system_s> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = system_s; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::basic_string<char>]?:
/tc_manager_node.cpp:74:41: required from here
/system.h:26:8: error: uninitialized member ?system_s::start_cmd? with ?const? type ?char* const [10]? [-fpermissive]
struct system_s {
^
system.h:26:8: error: uninitialized member ?system_s::end_cmd? with ?const? type ?char* const [10]? [-fpermissive]
In file included from /usr/include/c++/4.8/map:61:0,
from /opt/ros/indigo/include/ros/console.h:42,
from /opt/ros/indigo/include/ros/ros.h:40,
from
/tc_manager_node.cpp:2:
/usr/include/c++/4.8/bits/stl_map.h:469:59: 注意:合成方法 ?system_s::system_s()?首先需要在这里
__i = insert(__i, value_type(__k, mapped_type()));
为什么这个成员的类型是 system_s 'uninitialized'?它大概存储在 sys_map
中已经初始化。它与在 int add_system(const system_s &sys)
中将 system_s
作为参考传递有关吗?
正如@Greg Kikola 所说,const
成员 必须 被初始化。在这里查看如何使用初始化列表(不要与 std::initializer_list
混淆):http://en.cppreference.com/w/cpp/language/initializer_list
const 与指针的位置有时会令人困惑。 X * const p
表示:
“p is a const
pointer to an X that is non-const
”: you can’t change the pointer p itself, but you can change the X object via p. [source]
这意味着创建 system_s
的地址永远无法更改。这很糟糕,因为您不是 constructor-initializing start_cmd
或 end_cmd
这意味着可以为 10 个指针中的 none 分配一个有效地址。它们以未初始化的地址开头,永远不能分配任何其他内容。
编辑:
此 post 被标记为:c++03. There is no straight forward way to initialize arrays in C++03. You can look at this question for some workarounds: Initializing a member array in constructor initializer If you have the ability to go with c++11 you can use List Initialization。
如果找不到映射条目,map
的 operator[]
(您用 sys_map[msg->system]
调用)有可能创建一个新条目。新条目是 default-constructed,但您的 class 不是 default-constructible。
要解决此问题,请不要在地图上使用 []
。而是使用 find
来查找您要查找的条目。
我正在编写一个程序来传递我不想修改的结构类型。该结构有两个 const
成员,如下所示:
struct system_s {
std::string name;
std::string pkg;
char *const start_cmd[10];
char *const end_cmd[10];
bool ros;
bool equals(const system_s &cmp);
};
结构存储在具有以下格式的地图中。它是 class 成员:
std::map<std::string, system_s> sys_map;
还有一张临时地图。如果您愿意,可以将 sys_map
视为缓存。但实际上你不必担心它是如何被用于这个问题的。正在调用 sys_map
以将系统添加到临时地图,如下所示。它在 class 方法中:
add_system(sys_map[msg->system]);
(*)
这个函数有如下定义。这是一个class方法:
int add_system(const system_s &sys);
调用 (*) 时,出现以下错误:
system.h: In instantiation of ?std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::basic_string<char>; _Tp = system_s; _Compare = std::less<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, system_s> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = system_s; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::basic_string<char>]?:
/tc_manager_node.cpp:74:41: required from here
/system.h:26:8: error: uninitialized member ?system_s::start_cmd? with ?const? type ?char* const [10]? [-fpermissive]
struct system_s {
^
system.h:26:8: error: uninitialized member ?system_s::end_cmd? with ?const? type ?char* const [10]? [-fpermissive]
In file included from /usr/include/c++/4.8/map:61:0,
from /opt/ros/indigo/include/ros/console.h:42,
from /opt/ros/indigo/include/ros/ros.h:40,
from
/tc_manager_node.cpp:2: /usr/include/c++/4.8/bits/stl_map.h:469:59: 注意:合成方法 ?system_s::system_s()?首先需要在这里 __i = insert(__i, value_type(__k, mapped_type()));
为什么这个成员的类型是 system_s 'uninitialized'?它大概存储在 sys_map
中已经初始化。它与在 int add_system(const system_s &sys)
中将 system_s
作为参考传递有关吗?
正如@Greg Kikola 所说,const
成员 必须 被初始化。在这里查看如何使用初始化列表(不要与 std::initializer_list
混淆):http://en.cppreference.com/w/cpp/language/initializer_list
const 与指针的位置有时会令人困惑。 X * const p
表示:
“p is a
const
pointer to an X that is non-const
”: you can’t change the pointer p itself, but you can change the X object via p. [source]
这意味着创建 system_s
的地址永远无法更改。这很糟糕,因为您不是 constructor-initializing start_cmd
或 end_cmd
这意味着可以为 10 个指针中的 none 分配一个有效地址。它们以未初始化的地址开头,永远不能分配任何其他内容。
编辑: 此 post 被标记为:c++03. There is no straight forward way to initialize arrays in C++03. You can look at this question for some workarounds: Initializing a member array in constructor initializer If you have the ability to go with c++11 you can use List Initialization。
如果找不到映射条目,map
的 operator[]
(您用 sys_map[msg->system]
调用)有可能创建一个新条目。新条目是 default-constructed,但您的 class 不是 default-constructible。
要解决此问题,请不要在地图上使用 []
。而是使用 find
来查找您要查找的条目。