gpt4 book ai didi

c++ - 如何在 Visual Studio 2008 SP1 中使用 std::tr1::mem_fun?

转载 作者:搜寻专家 更新时间:2023-10-31 00:24:24 25 4
gpt4 key购买 nike

VS2008 SP1 文档讨论了 std::tr1::mem_fun .

那么为什么,当我尝试使用 std::tr1::mem_fun ,为什么会出现这个编译错误?:

'mem_fun' : is not a member of 'std::tr1'

同时,我可以使用 std::tr1::function 没有问题。

这是我尝试编译的示例代码,它应该调用 TakesIntTest 的实例上, 通过function<void (int)> :

#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::tr1::function<void (int)> f =
std::tr1::bind(std::tr1::mem_fn(&Test::TakesInt), t, std::tr1::placeholders::_1);
f(2);

Binder 需要 int 参数。所以你必须给它一个占位符,它代表生成的函数对象需要的整数参数。

顺便说一句:我不确定您是否已经知道这一点。但是你不需要那个 mem_fn 。只需将其更改为

std::tr1::function<void (int)> f =
std::tr1::bind(&Test::TakesInt, t, std::tr1::placeholders::_1);
f(2);

关于c++ - 如何在 Visual Studio 2008 SP1 中使用 std::tr1::mem_fun?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/293799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com