我得到錯誤:
"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;
}
uj5u.com熱心網友回復:
您的代碼有一些問題
首先,靜態成員的正確初始化
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);
}
}
查看演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/485501.html
上一篇:在C 中自動參考地址
下一篇:函子結構的C 型別
