2013-12-07 3 views
0

문제가 있습니다. 사용자 정의 함수 operator*을 구현했습니다.C++ 연산자가 오버로드되어 다른 연산자가 표시되지 않습니다.

헤더에서

:

class Matrix 
{ 
public: 
Matrix operator*(int arg); //(1) 
... 
} 

Matrix operator*(int a, const Matrix& m) 
{ 
    return m * a; //(2) 
} 

(1) 내가 MAIN.CPP이 작업을 수행 할 수 있습니다

Matrix a = Matrix::GetRandom..... 
Matrix b = a * 2; 

(2)이 라인에서 내가 갖는 컴파일러 오류 :

IntelliSense: no operator "*" matches these operandsnoperand types are: const Matrix * int

어떻게 수정합니까?

+0

또한 (비회원) 친구 기능 '친구 매트릭스 연산자 (매트릭스, INT) 등 모두 연산자를 쓸 수를' . 이것은 대칭을 유지하며 서로 옆에 선언됩니다. – dyp

답변

3

m 그렇게 만 const 방법은 그것을 호출 할 수 있습니다, const입니다. Matrix::operator*에게 const 멤버 함수 만들기 :;`와`친구 매트릭스 연산자 (INT, 매트릭스)

Matrix operator*(int arg) const; 
1

당신은 당신의 연산자 오버로드 선언에서 const을 놓치고 :

Matrix operator*(int arg) const; 
관련 문제