具有已实现功能的结构向量中的错误
Error in vector of struct with implemented Functions
我收到错误:
"no instance of constructor "std::vector<_Ty, _Alloc>::vector
[with _Ty=FunctionToUpdate, _Alloc=std::allocator<FunctionToUpdate>]" matches the argument list"
无论我如何更改它,只要我将它保留为 class,它就会一直存在。如果我将所有内容都保留在一个简单的 .cpp
中,而没有 class 和 header,那么一切都会很容易解决。
My.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;
};
这是我的.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;
}
您的代码有一些问题
首先,静态成员Error::table
的正确初始化如下:
const std::vector<FunctionToUpdate> Error::table
{
{ 100, &Error::testFunctionA, { { {177} }, { {"string"} } }},
{ 1948, &Error::testFunctionB, { { {314} }, { {"string"} } } }
};
注意语法&Error::testFunctionA
用于寻址成员函数指针。此外,Params
有两个向量。一个是 std::vector<int>
,另一个是 std::vector<std::string>
。在您的代码中,没有提到 std::vector<std::string>
。
在FunctionToUpdate
中成员函数指针类型错误。使用 typed member function pointer,您可以
// forward declaration
class Error;
// member function pointer type
using ErrorFunType = void(Error::*)(int, Params);
struct FunctionToUpdate
{
int Version;
ErrorFunType Function;
Params Parameters;
};
其次,Error::Error()
中成员函数指针的调用错误。 It needs an (Error
class) instance to call with。例如:
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);
// ...
}
以上更改将使您的代码 compiles again!
如果想知道,如何使用 std::function
处理指向成员函数的指针,(一种方法)包装实例以调用 std::function
类型的成员。
示例如下:
// forward declaration
class Error;
// member function pointer
using ErrorFunType = std::function<void(Error*, int, Params)>;
struct FunctionToUpdate
{
int Version;
ErrorFunType Function;
Params Parameters;
};
现在 Error::Error()
Error::Error()
{
for (auto functionToUpdate : table)
{
functionToUpdate.Function(this
, functionToUpdate.Version, functionToUpdate.Parameters);
}
}
我收到错误:
"no instance of constructor "std::vector<_Ty, _Alloc>::vector
[with _Ty=FunctionToUpdate, _Alloc=std::allocator<FunctionToUpdate>]" matches the argument list"
无论我如何更改它,只要我将它保留为 class,它就会一直存在。如果我将所有内容都保留在一个简单的 .cpp
中,而没有 class 和 header,那么一切都会很容易解决。
My.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;
};
这是我的.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;
}
您的代码有一些问题
首先,静态成员
Error::table
的正确初始化如下:const std::vector<FunctionToUpdate> Error::table { { 100, &Error::testFunctionA, { { {177} }, { {"string"} } }}, { 1948, &Error::testFunctionB, { { {314} }, { {"string"} } } } };
注意语法
&Error::testFunctionA
用于寻址成员函数指针。此外,Params
有两个向量。一个是std::vector<int>
,另一个是std::vector<std::string>
。在您的代码中,没有提到std::vector<std::string>
。
在
FunctionToUpdate
中成员函数指针类型错误。使用 typed member function pointer,您可以// forward declaration class Error; // member function pointer type using ErrorFunType = void(Error::*)(int, Params); struct FunctionToUpdate { int Version; ErrorFunType Function; Params Parameters; };
其次,
Error::Error()
中成员函数指针的调用错误。 It needs an (Error
class) instance to call with。例如: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); // ... }
以上更改将使您的代码 compiles again!
如果想知道,如何使用 std::function
处理指向成员函数的指针,(一种方法)包装实例以调用 std::function
类型的成员。
示例如下:
// forward declaration
class Error;
// member function pointer
using ErrorFunType = std::function<void(Error*, int, Params)>;
struct FunctionToUpdate
{
int Version;
ErrorFunType Function;
Params Parameters;
};
现在 Error::Error()
Error::Error()
{
for (auto functionToUpdate : table)
{
functionToUpdate.Function(this
, functionToUpdate.Version, functionToUpdate.Parameters);
}
}