简体   繁体   English

c ++ 11线程向量作为类成员

[英]c++11 vector of threads as class member

Can someone explain why I can't write a vector of threads like this: 有人可以解释为什么我不能写这样的线程向量:

//This is declared in a namespace
const uint MAXTHREADSAMOUNT = std::thread::hardware_concurrency();
//...

//declared in the same namespace
class AI {
    static vector<std::thread> Helpers(MAXTHREADSAMOUNT);
};

instead the compiler is forcing me to use this wierd looking method: 相反,编译器迫使我使用这个看起来很奇怪的方法:

class AI {
    static vector<std::thread> Helpers(std::thread);
};

The error message I get when compiling the first one is: 编译第一个时得到的错误信息是:

error: 'MAXTHREADSAMOUNT' is not a type

It has nothing to do with the vector being static, but I notice that the first method works if the vector is not declared inside a class or struct object. 它与向量是静态无关,但我注意到,如果向量未在类或结构对象中声明,则第一种方法有效。

So my question is why does vector need the type being stored to be passed explicitly via the constructor rather than using the type already declared in the template? 所以我的问题是为什么vector需要通过构造函数显式传递存储的类型而不是使用模板中已经声明的类型?

You can't initialize a static data member inline, the second version is wrong too, it's actually a function declaration that returns a vector of threads and takes a thread. 你不能内联初始化静态数据成员,第二个版本也是错误的,它实际上是一个函数声明,它返回一个线程向量并接受一个线程。 Just initialize it outside the class like your supposed to. 只需像你想象的那样在课堂外初始化它。

vector<std::thread> AI::Helpers(MAXTHREADSAMOUNT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM