매트릭스 클래스를 작성 중이며 함수 호출 연산자를 두 번 오버로드했습니다. 행렬의 핵심은 2D 이중 배열입니다. Windows 콘솔에서 MinGW GCC 컴파일러를 사용하고 있습니다.operator() 오버로드가 잘못되었습니다.
첫 번째 오버로드는 요소를보기 위해 배열에서 double을 반환하기위한 것입니다. 두 번째 오버로드는 해당 위치에서 데이터를 변경 (배열의 위치에 대한 참조를 반환하기위한 것입니다.
double operator()(int row, int col) const ; //allows view of element
double &operator()(int row, int col); //allows assignment of element
내가 테스트 루틴을 쓰고 있어요하고 "보기"과부하라는 결코 극복 것을 발견했다 . 어떤 이유로 다음의 printf() 문을 사용하는 참조를 반환 과부하를 호출하는 컴파일러 "기본값".
fprintf(outp, "%6.2f\t", testMatD(i,j));
은 내가없이 내 자신의 매트릭스 클래스를 작성하여 신들을 모욕하고있어 이해 벡터를 사용하고 CI/O 기능으로 테스트하기. 나는 내세에서 철저히 처벌되며, 필요는 없습니다. 그것 여기.
궁극적으로 나는 여기에서 무슨 일이 일어나고 있고 그것을 고치는 지 알고 싶습니다. 나는 멤버 함수보다는 클리너를 찾는 연산자 오버로드를 선호한다.
아이디어가 있으십니까?
매트릭스 클래스 : 관련없는 코드가 생략되었습니다.
class Matrix
{
public:
double getElement(int row, int col)const; //returns the element at row,col
//operator overloads
double operator()(int row, int col) const ; //allows view of element
double &operator()(int row, int col); //allows assignment of element
private:
//data members
double **array; //pointer to data array
};
double Matrix::getElement(int row, int col)const{
//transform indices into true coordinates (from sorted coordinates
//only row needs to be transformed (user can only sort by row)
row = sortedArray[row];
result = array[usrZeroRow+row][usrZeroCol+col];
return result;
}
//operator overloads
double Matrix::operator()(int row, int col) const {
//this overload is used when viewing an element
return getElement(row,col);
}
double &Matrix::operator()(int row, int col){
//this overload is used when placing an element
return array[row+usrZeroRow][col+usrZeroCol];
}
테스트 프로그램 : 관련없는 코드가 생략되었습니다. 개체 CONST 경우
int main(void){
FILE *outp;
outp = fopen("test_output.txt", "w+");
Matrix testMatD(5,7); //construct 5x7 matrix
//some initializations omitted
fprintf(outp, "%6.2f\t", testMatD(i,j)); //calls the wrong overload
}
이 프로그램이 C++ 인 경우 프로그램처럼 실행해야합니다. 'std :: vector', * not * 수동 메모리 관리를 사용하십시오. 'fopen' /'fclose' 대신에'fstream'을 사용하십시오 (힌트 : 후자가 빠짐). 또한'get' 함수를 사용하는 것이 이상하고, 하나의 과부하에 사용하고, 두 번째 과부하 (아무것도 읽지 않으면 행동 변경)와 아무런 관계가 없습니다. – GManNickG