重载运算符 += 和 + Matrix c++
Overload operators += and + Matrix c++
我在 C++ 中重载运算符 += 和 + 时遇到问题。我有以下内容,但我不知道为什么……:“二进制表达式的操作数无效(Matrice* 和 Matrice*)你知道为什么吗?谢谢。
注意:operator = 确实有效。
main.cpp
#include <iostream>
#include <cmath>
using namespace std ;
#include "Matrice.hpp"
int main () {
double *tableau = new double[2*2]{1,4,3,2} ;
double *tableau1 = new double[2*2]{1,6,4,-2} ;
Matrice * A = new Matrice(2, tableau) ;
Matrice * B = new Matrice(2, tableau1) ;
Matrice * C = new Matrice() ;
C = A+B ;} // error appears here
Matrice.cpp
#include "Matrice.hpp"
Matrice::Matrice(const Matrice& m) : dim(m.dim), coeffs(new double[m.dim*m.dim]){
for(int i=0; i<dim*dim; i++) {
coeffs[i] = m.coeffs[i] ;
}
}
Matrice::~Matrice() {
delete [] coeffs ;
}
Matrice.hpp
#ifndef Matrice_hpp
#define Matrice_hpp
#include <iostream>
using namespace std ;
class Matrice {
private :
unsigned int dim;
double * coeffs ;
public :
Matrice() {
dim = 2 ;
coeffs = new double[dim*dim] ;
for(int i=0; i<dim*dim; i++) {
coeffs[i] = 0 ;
}
}
Matrice(unsigned int n, double* v) : dim(n), coeffs(new double[dim*dim]) {
if(v) { for(int i=0; i<dim*dim; i++) { coeffs[i] = v[i] ;}
}
else { for(int i=0; i<dim*dim; i++) coeffs[i] = 0 ; }
}
Matrice(const Matrice&) ;
~Matrice () ;
int dimension() const {return dim;}
void modifier(int position, int valeur) {coeffs[position] = valeur ; }
Matrice& operator= (const Matrice& m) {
if(coeffs != m.coeffs) {
delete [] coeffs ;
dim = m.dim ;
coeffs = new double[m.dim*m.dim] ;
for(int i=0; i<m.dim*m.dim ; i++) {
coeffs[i] = m.coeffs[i] ;
}
}
return *this ;
}
Matrice& operator+=(const Matrice& m) {
for(int i=0; i<dim*dim; i++) {
coeffs[i] += m.coeffs[i] ;
}
return *this ;
}
Matrice&operator+ (const Matrice& m)
{
for(int i=0; i<dim*dim; i++) {
coeffs[i] = coeffs[i] + m.coeffs[i] ;
}
return *this ;
}
double* Coefficients() {return coeffs ;}
void Afficher() {
for(int i=0; i<dim*dim; i++) {
if (i%dim == 0) {cout << endl ; }
cout << coeffs[i] << " " ;
}
cout << endl ;
}
};
#endif /* Matrice_hpp */
当您在 class 中定义运算符时,它可以用作 class 的实例,而不是指针。因此,如果您将代码更改为:
Matrice A(2, tableau) ;
Matrice B(2, tableau1) ;
Matrice C = A + B;
应该可以。您还可以获得类似的取消引用指针的效果:
*C = *A + *B;
但是没有理由在您的代码中使用动态分配的对象。例如,您的代码中存在内存泄漏,因为您没有进行适当的清理(删除语句)。
您正在尝试添加指针。 A、B 和 C 是指针。
要使用 Their 运算符,您需要取消引用它们。
*C = *A + *B
会起作用。
我还没有完全检查任何副作用,但这将允许您的代码编译和 运行.
但是你的 Pointer 会乱七八糟的。
A 和 C 将包含相同的值。的系数正在获取 B 的值,然后 return 指向 A 的指针。
然后 C 将是指向 B 的指针。
您将需要涉及新声明才能制作真实副本。
您需要检查此项以避免不必要的错误。
我在 C++ 中重载运算符 += 和 + 时遇到问题。我有以下内容,但我不知道为什么……:“二进制表达式的操作数无效(Matrice* 和 Matrice*)你知道为什么吗?谢谢。
注意:operator = 确实有效。
main.cpp
#include <iostream>
#include <cmath>
using namespace std ;
#include "Matrice.hpp"
int main () {
double *tableau = new double[2*2]{1,4,3,2} ;
double *tableau1 = new double[2*2]{1,6,4,-2} ;
Matrice * A = new Matrice(2, tableau) ;
Matrice * B = new Matrice(2, tableau1) ;
Matrice * C = new Matrice() ;
C = A+B ;} // error appears here
Matrice.cpp
#include "Matrice.hpp"
Matrice::Matrice(const Matrice& m) : dim(m.dim), coeffs(new double[m.dim*m.dim]){
for(int i=0; i<dim*dim; i++) {
coeffs[i] = m.coeffs[i] ;
}
}
Matrice::~Matrice() {
delete [] coeffs ;
}
Matrice.hpp
#ifndef Matrice_hpp
#define Matrice_hpp
#include <iostream>
using namespace std ;
class Matrice {
private :
unsigned int dim;
double * coeffs ;
public :
Matrice() {
dim = 2 ;
coeffs = new double[dim*dim] ;
for(int i=0; i<dim*dim; i++) {
coeffs[i] = 0 ;
}
}
Matrice(unsigned int n, double* v) : dim(n), coeffs(new double[dim*dim]) {
if(v) { for(int i=0; i<dim*dim; i++) { coeffs[i] = v[i] ;}
}
else { for(int i=0; i<dim*dim; i++) coeffs[i] = 0 ; }
}
Matrice(const Matrice&) ;
~Matrice () ;
int dimension() const {return dim;}
void modifier(int position, int valeur) {coeffs[position] = valeur ; }
Matrice& operator= (const Matrice& m) {
if(coeffs != m.coeffs) {
delete [] coeffs ;
dim = m.dim ;
coeffs = new double[m.dim*m.dim] ;
for(int i=0; i<m.dim*m.dim ; i++) {
coeffs[i] = m.coeffs[i] ;
}
}
return *this ;
}
Matrice& operator+=(const Matrice& m) {
for(int i=0; i<dim*dim; i++) {
coeffs[i] += m.coeffs[i] ;
}
return *this ;
}
Matrice&operator+ (const Matrice& m)
{
for(int i=0; i<dim*dim; i++) {
coeffs[i] = coeffs[i] + m.coeffs[i] ;
}
return *this ;
}
double* Coefficients() {return coeffs ;}
void Afficher() {
for(int i=0; i<dim*dim; i++) {
if (i%dim == 0) {cout << endl ; }
cout << coeffs[i] << " " ;
}
cout << endl ;
}
};
#endif /* Matrice_hpp */
当您在 class 中定义运算符时,它可以用作 class 的实例,而不是指针。因此,如果您将代码更改为:
Matrice A(2, tableau) ;
Matrice B(2, tableau1) ;
Matrice C = A + B;
应该可以。您还可以获得类似的取消引用指针的效果:
*C = *A + *B;
但是没有理由在您的代码中使用动态分配的对象。例如,您的代码中存在内存泄漏,因为您没有进行适当的清理(删除语句)。
您正在尝试添加指针。 A、B 和 C 是指针。 要使用 Their 运算符,您需要取消引用它们。
*C = *A + *B
会起作用。 我还没有完全检查任何副作用,但这将允许您的代码编译和 运行.
但是你的 Pointer 会乱七八糟的。 A 和 C 将包含相同的值。的系数正在获取 B 的值,然后 return 指向 A 的指针。 然后 C 将是指向 B 的指针。 您将需要涉及新声明才能制作真实副本。
您需要检查此项以避免不必要的错误。