我的班级是由出向量的向量的矩阵,我似乎已经在我的构造问题:C++ - 模板类的拷贝构造函数
#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:
const unsigned int INITIAL_ROW_SIZE = 1;
const unsigned int INITIAL_COL_SIZE = 1;
Matrix() : _rows(INITIAL_COL_SIZE), _cols(INITIAL_COL_SIZE),
_matrix(1,vector<T>(1)) {
cout << "ctor" << endl;
}
Matrix (unsigned int rows, unsigned int cols) : _rows(rows), _cols(cols),_matrix(_rows,vector<T>(_cols) {
}
Matrix (const Matrix<T> &other) :Matrix(other._rows, other._cols)
{
for(int i = 0; i < _rows; i++)
{
copy(other._matrix[i].begin(), other._matrix[i].end(), _matrix[i]);
}
};
}
当我尝试使用的代码将无法编译拷贝构造函数, 在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);
^
我怀疑我的_Matrix应留作成员的initalization是不正确的,但我不知道。它也可以通过std :: copy但语法看起来正确。
'_Matrix应留作(_rows,矢量替代它'和'矩阵(常量矩阵&其他)在班级之外被宣布无效。我很惊讶这经历了解析阶段。 –
Quentin
@Quentin,这个错误是因为错误地使用了std :: copy()函数。 – v78
你有一堆与你问的问题无关的语法错误。假设'T'支持正常的复制语义(又名“像'int'”),根本没有必要定义复制构造函数。 'std :: vector's拷贝构造函数意味着'Matrix'的编译器生成拷贝构造函数将按需要工作。 – Peter