gpt4 book ai didi

c++ - QtConcurrent::blockingMapped() 和 std::vector<> 错误

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

QtConcurrent 似乎与 QT 容器(QListQVector)一起工作良好,但与声称的 in the documentation 相反,它与 STL 容器一起失败。

这是我想在我的容器上使用的虚拟函数:

void addOne(int & i)
{
++i;
}

int addOneC(const int & i)
{
return i+1;
}

有效的例子:

int main( int argc, char** argv )
{
// Qt containers
QList<int> l;
l << 1 << 2 << 4 << 3;
QList<int> l1 = QtConcurrent::blockingMapped(l, addOneC);
QtConcurrent::blockingMap(l1, addOne);

// Standard containers
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(4);
v.push_back(3);
QtConcurrent::blockingMap(v, addOne);
}

什么不起作用:

int main( int argc, char** argv )
{
// Standard containers
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(4);
v.push_back(3);
vector<int> v1 = QtConcurrent::blockingMapped(v, addOneC);
}

它会导致一个编译错误,其中包含非常长且令人困惑的模板错误。

如果有人知道原因,那真的很有帮助!谢谢!


错误日志:

1>C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xutility(442) : error C2825: '_Alloc': must be a class or namespace when followed by '::' 1> .\main.cpp(187) : see reference to class template instantiation 'std::_Container_base_aux_alloc_real<_Alloc>' being compiled 1> with 1> [ 1> _Alloc=int 1> ] 1>C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xutility(442) : error C2903: 'rebind' : symbol is neither a class template nor a function template 1>C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xutility(442) : error C2039: 'rebind' : is not a member of 'global namespace''
1>C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xutility(442) : error C2143: syntax error : missing ';' before '<'
1>C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xutility(442) : error C2039: 'other' : is not a member of '
global namespace'' 1>C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xutility(442) : error C2238: unexpected token(s) preceding ';' 1>.\main.cpp(187) : error C2248: 'std::_Container_base_aux_alloc_real<_Alloc>::~_Container_base_aux_alloc_real' : cannot access protected member declared in class 'std::_Container_base_aux_alloc_real<_Alloc>' 1> with 1> [ 1> _Alloc=int 1> ] 1> C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xutility(435) : see declaration of 'std::_Container_base_aux_alloc_real<_Alloc>::~_Container_base_aux_alloc_real' 1> with 1> [ 1> _Alloc=int 1> ] 1>.\main.cpp(187) : error C2440: 'initializing' : cannot convert from 'std::_Container_base_aux_alloc_real<_Alloc>' to 'std::vector<_Ty>' 1> with 1> [ 1> _Alloc=int 1> ] 1> and 1> [ 1> _Ty=int 1> ] 1> No constructor could take the source type, or constructor overload resolution was ambiguous

最佳答案

我认为您应该明确地将容器的类型指定给 blockingMapped

int main( int argc, char** argv )
{
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(4);
v.push_back(3);

std::vector<int> v1 = QtConcurrent::blockingMapped<std::vector<int> >(v, addOneC);
}

在你给出的简单例子中编译并给我预期的结果。

关于c++ - QtConcurrent::blockingMapped() 和 std::vector<> 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6761313/

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