Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
静态映射到函数指针 我试图在C++中实现类似于C语言的设置。其思想是拥有类属性的枚举和相应的setter。我的代码如下所示。我将在类定义中编写实现 #include <string> #include <map> enum ClassProperties { Id, Name }; class MyClass { public: int Id; std::string Name; public void SetValue(ClassProperties c, std::string value){ setters[c](this, value); } private: typedef void (* t_setter)(MyClass *, std::string); static void set_id(MyClass * obj, std::string value) { obj->Id = std::stoi(value); } static void set_name(MyClass * obj, std::string value) { obj->Name = value; } static std::map<ClassProperties, t_setter> setters = {{ClassProperties:: Id, set_id}, {ClassProperties::Name, set_name}}; }; #包括 #包括 枚举类属性 { 身份证,姓名 }; 类MyClass { 公众: int-Id; std::字符串名; 公共void SetValue(ClassProperties c,std::string value){ 设置器[c](此,值); } 私人: typedef void(*t_setter)(MyClass*,std::string); 静态void set_id(MyClass*obj,std::string值){ obj->Id=std::stoi(值); } 静态无效集_名称(MyClass*obj,std::字符串值){ 对象->名称=值; } 静态std::map setters={{ClassProperties::Id,set_Id},{ClassProperties::Name,set_Name}; };_C++ - Fatal编程技术网

静态映射到函数指针 我试图在C++中实现类似于C语言的设置。其思想是拥有类属性的枚举和相应的setter。我的代码如下所示。我将在类定义中编写实现 #include <string> #include <map> enum ClassProperties { Id, Name }; class MyClass { public: int Id; std::string Name; public void SetValue(ClassProperties c, std::string value){ setters[c](this, value); } private: typedef void (* t_setter)(MyClass *, std::string); static void set_id(MyClass * obj, std::string value) { obj->Id = std::stoi(value); } static void set_name(MyClass * obj, std::string value) { obj->Name = value; } static std::map<ClassProperties, t_setter> setters = {{ClassProperties:: Id, set_id}, {ClassProperties::Name, set_name}}; }; #包括 #包括 枚举类属性 { 身份证,姓名 }; 类MyClass { 公众: int-Id; std::字符串名; 公共void SetValue(ClassProperties c,std::string value){ 设置器[c](此,值); } 私人: typedef void(*t_setter)(MyClass*,std::string); 静态void set_id(MyClass*obj,std::string值){ obj->Id=std::stoi(值); } 静态无效集_名称(MyClass*obj,std::字符串值){ 对象->名称=值; } 静态std::map setters={{ClassProperties::Id,set_Id},{ClassProperties::Name,set_Name}; };

静态映射到函数指针 我试图在C++中实现类似于C语言的设置。其思想是拥有类属性的枚举和相应的setter。我的代码如下所示。我将在类定义中编写实现 #include <string> #include <map> enum ClassProperties { Id, Name }; class MyClass { public: int Id; std::string Name; public void SetValue(ClassProperties c, std::string value){ setters[c](this, value); } private: typedef void (* t_setter)(MyClass *, std::string); static void set_id(MyClass * obj, std::string value) { obj->Id = std::stoi(value); } static void set_name(MyClass * obj, std::string value) { obj->Name = value; } static std::map<ClassProperties, t_setter> setters = {{ClassProperties:: Id, set_id}, {ClassProperties::Name, set_name}}; }; #包括 #包括 枚举类属性 { 身份证,姓名 }; 类MyClass { 公众: int-Id; std::字符串名; 公共void SetValue(ClassProperties c,std::string value){ 设置器[c](此,值); } 私人: typedef void(*t_setter)(MyClass*,std::string); 静态void set_id(MyClass*obj,std::string值){ obj->Id=std::stoi(值); } 静态无效集_名称(MyClass*obj,std::字符串值){ 对象->名称=值; } 静态std::map setters={{ClassProperties::Id,set_Id},{ClassProperties::Name,set_Name}; };,c++,C++,我希望这段代码选择cooResponse函数并调用它,但在最后一行我得到了一个错误 error: in-class initialization of static data member ‘std::map<ClassProperties, void (**)(MyClass*, std::basic_string<char>)> MyClass::setters’ of incomplete type error: could not convert ‘{{Id,

我希望这段代码选择cooResponse函数并调用它,但在最后一行我得到了一个错误

error: in-class initialization of static data member ‘std::map<ClassProperties, void (**)(MyClass*, std::basic_string<char>)> MyClass::setters’ of incomplete type

error: could not convert ‘{{Id, MyClass::set_id}, {Name, MyClass::set_name}}’ from ‘<brace-enclosed initializer list>’ to ‘std::map<ClassProperties, void (**)(MyClass*, std::basic_string<char>)>’
错误:不完整类型的静态数据成员“std::map MyClass::setters”的类内初始化
错误:无法将“{Id,MyClass::set_Id},{Name,MyClass::set_Name}}”从“”转换为“std::map”

我做错了什么?

您对
设置器的初始化是错误的。您应按以下方式进行更改:

1) 从类主体中删除行:

static std::map<ClassProperties, t_setter> setters =
    {{ClassProperties:: Id, set_id}, {ClassProperties::Name, set_name}};
std::map<ClassProperties, MyClass::t_setter> MyClass::setters = {
        { ClassProperties::Id, &MyClass::set_id },
        { ClassProperties::Name, &MyClass::set_name }
};
static std::map setters=
{{ClassProperties::Id,set_Id},{ClassProperties::Name,set_Name};
2) 将静态成员的适当初始化添加到类主体之外:

static std::map<ClassProperties, t_setter> setters =
    {{ClassProperties:: Id, set_id}, {ClassProperties::Name, set_name}};
std::map<ClassProperties, MyClass::t_setter> MyClass::setters = {
        { ClassProperties::Id, &MyClass::set_id },
        { ClassProperties::Name, &MyClass::set_name }
};
std::map MyClass::setters={
{ClassProperties::Id,&MyClass::set_Id},
{ClassProperties::Name,&MyClass::set_Name}
};
此外,您必须在
SetValue
函数之前删除公共说明符


clang++给出了一个更直接的“错误:非常量静态数据成员必须被初始化为不符合要求的”一旦你完成了这项工作,你可能想查看“成员函数指针”,这样你就可以使
set\u id
set\u name
成为非静态函数,并且可以被称为
obj.set\u id(值)顺便说一句。这看起来不像C#setters。非常感谢,但现在我得到了
MyClass::setters'`@Vahagn的多个定义。我添加了一个wandbox示例。