꽤 기초적이고 간단한 것들을 포함하는 매트릭스의 템플릿 클래스를 수행하는 숙제가 있습니다. 또한 표준 방식으로 동작하는 iterator 클래스 (중첩 된 클래스)를 만들어야하며, 특별히 복사 생성자를 지원해야합니다.템플릿의 중첩 클래스 생성자 관련 문제 (copy ctor가 다른 ctor를 덮어 쓰는 것처럼 보임)
template<class T>
class Matrix
{
public:
//nested iterator class
class iterator
{
public:
typedef iterator self_type;
typedef T value_type;
typedef T& reference;
typedef T* pointer;
typedef std::vector<T>& vector;
iterator(Matrix &other, int index) :
_currIndex(index), _currMatrix(other)
{
}
iterator(iterator& other) :
_currIndex(other._currIndex), _currMatrix(other._currMatrix)
{
}
private:
int _currIndex;
Matrix<T>& _currMatrix;
}
//function that creates an iterator for the current matrix
iterator begin()
{
return iterator(*this, 0);
}
iterator end()
{
return iterator(*this, _data.size());
}
private:
unsigned int _rows;
unsigned int _cols;
vector<T> _data;
}
매트릭스 복사 등의 여러 생성자, 빈 등 그들은 개인 회원과 아무것도를 초기화가 있습니다
여기에 관련 matrix.h 코드입니다. 반복자 클래스도 ++ 연산자를 오버로드 내가 직면하고있어 문제는 ++ g을 사용하여 Linux에 다음 코드를 컴파일 함께
: 코드 컴파일과 확인을 실행 Visual Studio에서 창에
for(auto it = m.begin(); it != m.end(); it++)
{
cout << *it;
}
, 문제 없습니다. 리눅스에 다음과 같은 오류를 컴파일 할 때하는 팝업 : 나는 반복자 클래스의 복사 생성자를 주석 경우
debug.cpp: In function ‘int main()’:
debug.cpp:63:24: error: no matching function for call to ‘Matrix<int>::iterator::iterator(Matrix<int>::iterator)’
for (auto it = m.begin(); it != m.end(); it++)
^
debug.cpp:63:24: note: candidates are:
In file included from debug.cpp:11:0:
matrix.h:35:3: note: Matrix<T>::iterator::iterator(Matrix<T>::iterator&) [with T = int]
iterator(iterator& other) :
^
matrix.h:35:3: note: no known conversion for argument 1 from ‘Matrix<int>::iterator’ to ‘Matrix<int>::iterator&’
matrix.h:29:3: note: Matrix<T>::iterator::iterator(Matrix<T>, int) [with T = int]
iterator(Matrix other, int index) :
^
matrix.h:29:3: note: candidate expects 2 arguments, 1 provided
가, 다음 코드는 리눅스 (창문)에 잘 컴파일합니다. 두 생성자를 모두 유지하면 g ++에서 오류가 발생합니다. 그것의 복사본 생성자가 이전 생성자를 오버라이드하는 것처럼, 나는 왜 그 이유가 모릅니다.
왜 이런 일이 발생했는지에 대한 통찰력을 공유 할 수 있습니까? 어쩌면 내가 고칠 수 있을까?
참조 매개 변수에'const'가 누락 되었습니까? – Columbo