gpt4 book ai didi

c++ - boost 线程 : Term does not evaluate to function taking 0 arguments

转载 作者:行者123 更新时间:2023-11-27 23:54:03 25 4
gpt4 key购买 nike

版本信息:我使用的是 boost 1.64 和 Visual Studio 2015

我很难理解这个 boost 编译错误。

我的示例代码是:

#include <iostream>
#include <boost\thread.hpp>

using namespace std;

void testfunc(vector<int> &first, vector<string> &second)
{
first.push_back(42);
cout << "I live." << endl;
}

int main()
{
vector<int> first;
vector<string> second;
unique_ptr<boost::thread> thr = make_unique<boost::thread>(new boost::thread(testfunc, boost::ref(first), boost::ref(second)));

return 0;
}

编译失败并出现错误:boost\thread\detail\thread.hpp(116): error C2064: term does not evaluate to a function taking 0 arguments

为什么它试图将其编译为采用 0 个参数的函数?传递给 boost::thread 构造函数的两个额外参数不应该告诉 boost testfunc 需要的参数数量吗?

编辑:

问题似乎不是boost::thread构造函数,而是包裹指针的unique_ptr。没有它,代码编译。至于为什么会这样,我还是一头雾水。

最佳答案

这与 Boost、 vector 、字符串或线程无关。

您正在使用 make_unique<T>这里错了:

make_unique<boost::thread>(new boost::thread(testfunc, boost::ref(first), boost::ref(second)));
// ^^^^^^^^^^^^^^^^^^

它需要参数来构造 T ,但是您向它传递了一个指向已构造对象的原始指针。 boost::thread不知道如何处理 boost::thread* .

你应该简单地写:

make_unique<boost::thread>(testfunc, boost::ref(first), boost::ref(second));

关于c++ - boost 线程 : Term does not evaluate to function taking 0 arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43978434/

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