【发布时间】:2023-03-30 11:35:02
【问题描述】:
VS2008 SP1 文档谈到了 std::tr1::mem_fun。
那么,当我尝试使用 std::tr1::mem_fun 时,为什么会出现这个编译错误?:
'mem_fun' : is not a member of 'std::tr1'
同时,我可以毫无问题地使用std::tr1::function。
这是我正在尝试编译的示例代码,它应该通过function<void (int)> 在Test 的实例上调用TakesInt:
#include "stdafx.h"
#include <iostream>
#include <functional>
#include <memory>
struct Test { void TakesInt(int i) { std::cout << i; } };
void _tmain()
{
Test* t = new Test();
//error C2039: 'mem_fun' : is not a member of 'std::tr1'
std::tr1::function<void (int)> f =
std::tr1::bind(std::tr1::mem_fun(&Test::TakesInt), t);
f(2);
}
我正在尝试使用 mem_fun 的 tr1 版本,因为使用 std::mem_fun 时,我的代码也无法编译!我无法从编译器错误中判断问题是出在我的代码上,还是可以通过使用 tr1 的 mem_fun 来解决。那是你的 C++ 编译器错误(或者可能只是我!)。
更新:正确。答案是正确拼写为 mem_fn!
但是,当我修复它时,代码仍然无法编译。
这是编译器错误:
error C2562:
'std::tr1::_Callable_obj<_Ty,_Indirect>::_ApplyX' :
'void' function returning a value
【问题讨论】:
-
使用 std::mem_fun 时遇到的编译错误是什么?