[英]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"
無論我如何更改它,它都會持續存在,只要我將其保留為一個類。 如果我將它全部保存在一個沒有類和標題的簡單.cpp
中,那么一切都很容易解決。 我的.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
中,成員函數指針類型錯誤。 使用類型化成員函數指針,您可以
// forward declaration class Error; // member function pointer type using ErrorFunType = void(Error::*)(int, Params); struct FunctionToUpdate { int Version; ErrorFunType Function; Params Parameters; };
其次,在Error::Error()
中對指向成員函數的指針的調用是錯誤的。 它需要一個( Error
類)實例來調用。 例如:
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); // ... }
上述更改將進行,您的代碼再次編譯!
如果想知道,如何使用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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.