[英]Error in vector of struct with implemented Functions
I get the Erro:我得到错误:
"no instance of constructor "std::vector<_Ty, _Alloc>::vector
[with _Ty=FunctionToUpdate, _Alloc=std::allocator<FunctionToUpdate>]" matches the argument list"
No matter how I change it, it persists, as long I keep it as a class.无论我如何更改它,它都会持续存在,只要我将其保留为一个类。 If I keep it all in just a simple .cpp
without class and header, it all resolves easily.如果我将它全部保存在一个没有类和标题的简单.cpp
中,那么一切都很容易解决。 My.h:我的.h:
#include <vector>
#include <functional>
#include <iostream>
struct Params
{
std::vector<int> Integers;
std::vector<std::string> Strings;
};
struct FunctionToUpdate
{
int Version;
std::function<void(int, Params)> Function;
Params Parameters;
};
class Error
{
public:
Error();
void testFunctionA(int a, Params p);
void testFunctionB(int a, Params p);
protected:
const static std::vector<FunctionToUpdate> table;
};
Here is my .cpp
, please assist me, I can't find the error:这是我的.cpp
,请帮助我,我找不到错误:
#include "ErrorHandling.h"
Error::Error()
{
for (auto functionToUpdate : table)
{
functionToUpdate.Function(functionToUpdate.Version, functionToUpdate.Parameters);
std::cout << "############################################" << std::endl;
}
std::cout << "Done!" << std::endl;
}
void Error::testFunctionA(int a, Params parameter)
{
//std::cout << "Size Integers: " << parameter.Integers.size() << std::endl;
//std::cout << "Size Strings: " << parameter.Strings.size() << std::endl;
std::cout << a << std::endl;
for (auto& integer : parameter.Integers)
{
std::cout << integer << std::endl;
}
for (auto& integer : parameter.Strings)
{
std::cout << integer << std::endl;
}
}
void Error::testFunctionB(int a, Params parameter)
{
std::cout << a << std::endl;
std::cout << parameter.Integers.at(0) << std::endl;
}
const std::vector<FunctionToUpdate> Error::table
{ // <-- here the Error happens
{ 100, &testFunctionA, { {177}}},
{ 1948, &testFunctionB, { {314}}},
};
int main()
{
Error error;
}
Your code has a few issues您的代码有一些问题
First, the correct initialization of static member Error::table
would be as follows:首先,静态成员Error::table
的正确初始化如下:
const std::vector<FunctionToUpdate> Error::table { { 100, &Error::testFunctionA, { { {177} }, { {"string"} } }}, { 1948, &Error::testFunctionB, { { {314} }, { {"string"} } } } };
Note that the syntax &Error::testFunctionA
for addressing the member function pointer.请注意语法&Error::testFunctionA
用于寻址成员函数指针。 Additionally, the Params
has two vectors.此外, Params
有两个向量。 One is std::vector<int>
and the other is std::vector<std::string>
.一个是std::vector<int>
,另一个是std::vector<std::string>
。 In your code, the std::vector<std::string>
has not been mentioned.在您的代码中,没有提到std::vector<std::string>
。
In FunctionToUpdate
the member function pointer type is wrong.在FunctionToUpdate
中,成员函数指针类型错误。 Using typed member function pointer , you could使用类型化成员函数指针,您可以
// forward declaration class Error; // member function pointer type using ErrorFunType = void(Error::*)(int, Params); struct FunctionToUpdate { int Version; ErrorFunType Function; Params Parameters; };
Secondly, the call to pointer to the member function in Error::Error()
is wrong.其次,在Error::Error()
中对指向成员函数的指针的调用是错误的。 It needs an ( Error
class) instance to call with . 它需要一个( Error
类)实例来调用。 For example:例如:
for (auto functionToUpdate : table) { (this->*functionToUpdate.Function)( functionToUpdate.Version, functionToUpdate.Parameters ); // or more generic `std::invoke` (since c++17) // std::invoke(functionToUpdate.Function // , this, functionToUpdate.Version // , functionToUpdate.Parameters); // ... }
The above changes will make, your code compiles again !上述更改将进行,您的代码再次编译!
In case of wondering, how to handle the pointer to member function with std::function
, (one way) to wrap the instance to call the member along with the std::function
type.如果想知道,如何使用std::function
处理指向成员函数的指针,(一种方法)包装实例以调用成员以及std::function
类型。
Following is the example:以下是示例:
// forward declaration
class Error;
// member function pointer
using ErrorFunType = std::function<void(Error*, int, Params)>;
struct FunctionToUpdate
{
int Version;
ErrorFunType Function;
Params Parameters;
};
now in Error::Error()
现在在Error::Error()
Error::Error()
{
for (auto functionToUpdate : table)
{
functionToUpdate.Function(this
, functionToUpdate.Version, functionToUpdate.Parameters);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.