2015-01-24 6 views
0

꽤 기초적이고 간단한 것들을 포함하는 매트릭스의 템플릿 클래스를 수행하는 숙제가 있습니다. 또한 표준 방식으로 동작하는 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 ++에서 오류가 발생합니다. 그것의 복사본 생성자가 이전 생성자를 오버라이드하는 것처럼, 나는 왜 그 이유가 모릅니다.

왜 이런 일이 발생했는지에 대한 통찰력을 공유 할 수 있습니까? 어쩌면 내가 고칠 수 있을까?

+6

참조 매개 변수에'const'가 누락 되었습니까? – Columbo

답변

4

만 lvalues가 const가 아닌 참조에 바인드하지만 임시 객체를 rvalue 그리고 그들은 비에 바인딩 할 수 없습니다 수 있기 때문에 복사 생성자의 const 중요하다 -const 참조. 서명을 다음과 같이 변경해야합니다.

iterator(const iterator& other) 

Visual C++에서는 기본적으로 "비표준 확장 사용"경고를 내 보냅니다.

자세한 내용은 Herb Sutter's post을 읽어 보시기 바랍니다.

+0

thx로 고정 시켰습니다. 그것은 내 부분에 대한 지식이 부족한 문제 였으므로 잘못된 점을 정리해 주셔서 감사합니다! – user475680

1

임시로 const& (또는 값으로)을 전달할 수 있습니다. Visual Studio는이 시점에서 잘못되었습니다. 당신의 복사 생성자의 적절한 서명은 다음과 같습니다

iterator(const iterator& other) 
+1

temporaries는 rvalue-reference로 전달할 수도 있습니다. 복사하는 것이 이동하는 것보다 비용이 많이 들지 않으면 도움이되지 않지만. – Deduplicator

+0

@Deduplicator : 어떻게 든 그 사소한 사건을 놓쳤습니다. – erenon