행렬 프로그램에 대해 "+"연산자를 오버로드하려고합니다. 여기 내 코드가있어, 나에게 괜찮아 보인다. 그러나 주 기능에 2 개의 행렬을 추가하면 아무 일도 일어나지 않습니다. 누군가 도움을 줄 수 있습니까? 감사합니다 :) BTWC++ overloading : 요소로 행렬을 더하기위한 연산자 +
:
년 - 프로그램을 컴파일하고 행렬에 추가해야하는 지점까지 잘 실행됩니다.
- 나는 문제가 코드의 add (Mtrx, Mtrx) 함수에 테스트를 복사했기 때문에 내 operator +() - function의 구현에 있다고 가정합니다.
//Mtrx.h
#ifndef MTRX_H_
#define MTRX_H_
#include <iostream>
#include <string>
using namespace std;
using std::ostream;
class Mtrx {
int lines,cols;
float **p;
public:
Mtrx();
Mtrx(int,int);
int getLines();
int getCols();
float getElement(int,int);
void setLines(int);
void setCols(int);
void setElement(int,int,float);
Mtrx operator+(Mtrx&);
~Mtrx();
};
ostream& operator<<(ostream& os, Mtrx& m);
#endif /* MTRX_H_ */
//Mtrx.cpp는
//...
//...
Mtrx::~Mtrx(){
delete p;
p = NULL;
}
Mtrx Mtrx::operator+(Mtrx& m){
if(this->getLines() == m.getLines() && this->getCols() == m.getCols()){
Mtrx res(getLines(),getCols());
for (int i = 1; i <= this->getLines(); i++){
for(int j = 1; j <= this->getCols(); j++){
res.setElement(i,j,(this->getElement(i,j)+m.getElement(i,j)));
}
}
return res;
}
'1..nLines' 및 1..nCols의 반복은 의심스러워 보입니다. 이것은 의도적입니까, 아니면 0에서 시작한다는 뜻입니까? – kfmfe04
메인()도 붙일 수 있습니까? 연산자 + 확인을 보인다. – Amar