Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.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++ 设置std::function的调用约定_C++_Stl_Calling Convention - Fatal编程技术网

C++ 设置std::function的调用约定

C++ 设置std::function的调用约定,c++,stl,calling-convention,C++,Stl,Calling Convention,我对使用std非常陌生,目前我正在尝试调用一个函数,该函数将std::function作为参数。类似于以下内容: 在一个库中的.h文件中: typedef bool t (/*Params*/); void __stdcall Foo(std::function<t> &function) {m_function = function;} std::function<t> m_function; typedef bool t(/*Params*/); voi

我对使用std非常陌生,目前我正在尝试调用一个函数,该函数将std::function作为参数。类似于以下内容:

在一个库中的.h文件中:

typedef bool t (/*Params*/);

void __stdcall Foo(std::function<t> &function) {m_function = function;}

std::function<t> m_function;
typedef bool t(/*Params*/);
void u stdcall Foo(std::function&function){m_function=function;}
std::函数m_函数;
我导入了库并尝试在另一个cpp文件中使用Foo:

bool Implementation (/*Params*/)
{
   // Implementation
}

void Bar()
{
    Foo(std::function<t> (Implementation));
}
bool实现(/*Params*/)
{
//实施
}
空条()
{
Foo(std::function(Implementation));
}
由于调用约定,我在为x86(而不是x64)编译时遇到链接器错误(LNK2019):

Unresolved External Symbol __stdcall Foo (class std::tr1::function<bool __cdecl(/*Params*/) const&)
未解析的外部符号\uu stdcall Foo(类std::tr1::function尝试:

void Foo(const std::function&func)
{
func();
}
bool实现(/*Params*/)
{

考虑到您说过64位版本可以工作,首先检查您是否链接了库的正确版本(32位)。库链接正确(并且正确解析了其他函数)。我认为问题更多地与x64具有更简单的调用约定有关。关于正确导出的函数,需要注意的另一件事是,我能够为它们分配一个调用约定,就像我尝试将调用约定添加到我的类型定义时一样(或者在std::function中,我得到一个函数错误。错误告诉您没有
Foo
的定义,只是头文件中的一个声明。您需要
Foo
的定义(在您的库中?)你需要对此进行链接。我意识到我上面的代码有点误导。实际上,我在标题中有Foo的定义。Foo所做的一切都是设置一个成员变量。我将在上面更正此问题。我尝试了你的一些建议(删除了typedef,并通过了&implementation而不是implementation)我还尝试显式地为stdcall创建一个指针并传递它,然后它继续查找cdecl。接下来我将尝试转储lib并查看一下它将函数导出为什么。
void Foo(const std::function<bool()> &func)
{
  func();
}

bool Implementation (/*Params*/)
{
  cout << "Implementation"<<endl;
  return true;
}

void Bar()
{
    Foo(std::function<bool()>(&Implementation));
}

int main()
{
  Bar();
  return 0;
}