我一直在尝试更新我继承的一些托管 C++ 代码。我不懂任何 C++,但我在 1.1 的日子里上过一门 C# 课,所以我可以在某种程度上找到绕过 .Net 的方法。到目前为止,我使用 ConcurrentQueue 将工作从我的主线程发送到工作线程已经取得了很好的效果:
fullQueue = gcnew ConcurrentQueue<int>();
..
fullQueue->Enqueue(someNumber);
现在我想尝试插入实际对象,以便我可以向工作人员发送更复杂的指令。但是,这不起作用:
public ref class workUnit
{
int ptrOffset;
System::String^ outputPath;
public:
workUnit(int offset, System::String^ path)
{
ptrOffset=offset;
outputPath=path;
}
};
..
ConcurrentQueue test = gcnew ConcurrentQueue<workUnit ^>();
我得到:
'System::Collections::Concurrent::ConcurrentQueue' : use of class generic requires generic argument list
'System::Collections::Concurrent::ConcurrentQueue::ConcurrentQueue' : the function template cannot convert parameter 1 from type 'System::Collections::Concurrent::ConcurrentQueue<T> ^'
很明显,我遗漏了有关如何将对象插入队列的一些基本知识。在我的脑海中,我认为我正在创建一个队列,该队列将保存对我稍后可以实例化的类对象的引用,因此 CLR 应该只需要知道将进入的引用类型,但显然这是不正确的。我错过了什么?
最佳答案
好像你错过了什么:)
using namespace System::Collections::Concurrent;
ref class WorkUnit
{
};
int main()
{
ConcurrentQueue<WorkUnit^>^ test = gcnew ConcurrentQueue<WorkUnit^>();
}
您必须在初始化表达式的两边引用泛型类型
关于.net - 在托管 c++/.net 4.0 中创建对象的 ConcurrentQueue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13547753/