我正在创建一个需要调用 compute
的多线程类方法.
void compute(const Me::MyClass<T>& c1, Me:: MyClass<T>& target,std::size_t start);
namespace Me{
template<typename T> class MyClass{
computeMultiThreaded(){
MyClass<T> target = MyClass();
std::size_t n_threads = std::thread::hardware_concurrency();
std::vector<std::tuple<std::size_t, std::size_t>> parts = split_job(n_threads, number_jobs);
std::vector<std::thread> threads;
for (std::size_t ti = 0; ti < n_threads; ti++)
{
// , parts[ti], minCol, this, m2, returnMatrix));
threads.push_back(std::thread(compute,parts[ti]));
}
}
}
}
void compute(const Me::MyClass<T>& c1, Me:: MyClass<T>& target,std::size_t start){
...
}
现在,当我尝试用 compute
编译它时在 MyClass
之后定义, Me::MyClass
在 compute
的第一个定义中未知.当我删除第一个声明时,compute
创建线程时会不会知道?
我该如何解决这个问题 22?
error: use of undeclared identifier 'Me'
最佳答案
声明MyClass
在声明compute
之前.和 compute
如果您希望它具有任意 MyClass<T>
,则需要是一个函数模板参数类型。
namespace Me
{
template<typename T>
class MyClass;
}
template<typename T>
void compute(const Me::MyClass<T>& c1, Me:: MyClass<T>& target,std::size_t start);
关于c++ - 前向声明困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45928584/