简体   繁体   中英

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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