我的类是由向量向量组成的矩阵,我的构造函数似乎有问题:
Matric<int> m(5,5); Matrix<int> n=(m);
当我尝试使用复制构造函数时,代码将无法编译,
与 In file included from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/vector:60:0,
from /cppex3/ex3/Matrix.hpp:8,
from /cppex3/ex3/Tester.cpp:6:
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algobase.h: In instantiation of '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = const std::vector<int, std::allocator<int> >*; _OI = std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >]':
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algobase.h:438:45: required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<const std::vector<int, std::allocator<int> >*, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >; _OI = std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >]'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algobase.h:471:8: required from '_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<const std::vector<int, std::allocator<int> >*, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >; _OI = std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >]'
/ex3/Matrix.hpp:42:13: required from 'Matrix<T>::Matrix(const Matrix<T>&) [with T = int]'
/ex3/Tester.cpp:25:20: required from here
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algobase.h:394:57: error: no type named 'value_type' in '[01mstruct std::iterator_traits<std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >'
typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
^
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algobase.h:399:9: error: no type named 'value_type' in 'struct std::iterator_traits<std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >'
&& __are_same<_ValueTypeI, _ValueTypeO>::__value);
^
中一样,因为:
https://pbs.twimg.com/profile_images/1580483969/parisjs_transparent.png
我怀疑我对_matrix成员的初始化是不正确的,但我不确定。它也可以通过std :: copy但语法似乎是正确的。
答案 0 :(得分:1)
尝试
copy(other._matrix[i].begin(), other._matrix[i].end(),
std::back_inserter(_matrix[i]));
或
Matrix (const Matrix<T> &other)
: _rows{other._rows}, _cols{other._cols}, _matrix{other._matrix}
{
}
答案 1 :(得分:-2)
尝试使用此代码,它可以正常工作
替换
copy(other._matrix[i].begin(), other._matrix[i].end(), _matrix[i]);
<强> 与 强>
copy(other._matrix[i].begin(), other._matrix[i].end(), _matrix[i].begin());
以下是完整的代码。
#include <vector>
#include <exception>
#include <iostream>
using namespace std;
template<class T>
class Matrix
{
private:
unsigned int _rows;
unsigned int _cols;
vector <vector<T> > _matrix;
public:
Matrix () : _rows(1), _cols(1),
_matrix(1,vector<T>(1))
{
cout << "ctor" << endl;
}
Matrix (unsigned int rows, unsigned int cols) : _rows(rows), _cols(cols)
{
_matrix=vector< vector<T> >(rows,vector<T>(cols));
}
Matrix (const Matrix<T> &other)
: _rows{other._rows}, _cols{other._cols}, _matrix{other._matrix}
{
for(int i = 0; i < _rows; i++)
{
std::copy(other._matrix[i].begin(), other._matrix[i].end(), _matrix[i].begin());
}
}
};
int main(int argc, char const *argv[])
{
Matrix<int> m(5,5);
Matrix<int> n1(m);
Matrix<int> n2(m);
return 0;
}
另外
Matrix (const Matrix<T> &other) :Matrix(other._rows, other._cols);
将其替换为
Matrix (const Matrix<T> &other) {
Matrix(other._rows, other._cols);
...