c++ - 前向声明困惑

标签 c++ multithreading c++11

我正在创建一个需要调用 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::MyClasscompute 的第一个定义中未知.当我删除第一个声明时,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/

相关文章:

multithreading - OS X libstdc++可以防止boost::thread中断?

c++ - 为什么无法在 C++ 中撤消 'using'?

c++ - 如何静态断言 std::array 类成员在 c++11 中进行排序?

c++ - LeetCode #617 "Merge Two Binary Trees"使用 C++

multithreading - TTask 比 TThread 慢

c++ - 智能指针无法释放内存

multithreading - Ada任务和终止

c++ - 通过 Python 控制台对 PythonQt 库进行非锁定调用

c++ - 继承是这样吗?

c++ - 为 QML 的 ListView 公开 QObjects 的 QAbstractListModel。好的做法?