在我的泛型Matrix(T)类中,我使用静态函数重载operator +使用线程,这个函数会添加到这样的单行:
template <class T>
static void addToRow(vector<T> &dest, const vector<T> &source, int pos, int colls)
{
typename vector<T>::iterator itDest = dest.begin() + pos;
typename vector<T>::iterator itSource = source.cend() + pos;
int counter = 0;
while (counter != colls)
{
*itDest++ += *itSource++;
++counter;
}
}
此功能在课堂外实施。
在运算符+重载内部我试图像这样调用它:
const Matrix<T> operator+(const Matrix<T> &other) const
{
Matrix<T> result(*this);
vector<std::thread> threads;
int i, pos;
std::thread thrd;
for (i = 0; i < _rows; ++i)
{
pos = i*_colls;
threads.push_back(std::thread(&addToRow<T>, ref(_matrix), cref(other._matrix), pos, _colls));
}
typename vector<std::thread>::iterator it;
for (it = threads.begin(); it != threads.end(); ++it)
{
it->join();
}
return result;
}
和行:
threads.push_back(std::thread(&addToRow<T>, std::ref(*this), _matrix, other._matrix, pos, _colls));
我收到错误:
/cygdrive/c/Users/Roy/Desktop/semester s year 1/CPP/ex3/Matrix.hpp:201:102: required from 'const Matrix<T> Matrix<T>::operator+(const Matrix<T>&) const [with T = int]'
/cygdrive/c/Users/Roy/Desktop/semester s year 1/CPP/ex3/main.cpp:44:31: required from here
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/functional:1665:61: error: no type named 'type' in 'class std::result_of<void (*(const Matrix<int>*, std::vector<int, std::allocator<int> >, std::vector<int, std::allocator<int> >, int, int))(std::vector<int, std::allocator<int> >&, const std::vector<int, std::allocator<int> >&, int)>'
typedef typename result_of<_Callable(_Args...)>::type result_type;