c++ - C++ 中的简单运算符重载 =

标签 c++

我的代码存在问题,没有显示明显的错误。我创建了一个 vector 和矩阵类。问题在于当我尝试重载 Matrix 类的 = 运算符时。例如,如果 A 和 B 是两个矩阵,我希望操作 B = A 来修改 B,使其变为 A。这适用于 vector ,但对于矩阵,我有一个无法解决的“段错误”错误:这是内存问题或禁止修改,但我看不到。

// My code
#include <iostream>
#include <cmath>

using namespace std;

class Vecteur{
private:
    float *tab;
    int dim;

public:
    void affiche();
    Vecteur(int);
    Vecteur(float*, int);
    ~Vecteur();
    Vecteur(const Vecteur &);
    Vecteur operator=(const Vecteur &);
    Vecteur operator+(const Vecteur &);
    Vecteur operator-(const Vecteur &);
    float & operator[](int);
    Vecteur subvec(int, int);
    int getDimension(); //utile pour la fonction dot
    friend Vecteur operator*(float, const Vecteur &);
    Vecteur(); //Constructeur vide pour la class Matrice
    friend class Matrice;
 };

void Vecteur::affiche(){
    cout << "Le tenseur est de dimension :"<<dim<<endl;
    cout << "Voici le contenu du tenseur :\n";
    for(int i=0;i<dim;i++){
        cout <<tab[i]<<"\n";
    }
}

Vecteur::Vecteur(int taille){
    dim = taille;
    tab = new float[taille];
    for(int i=0;i<dim;i++){
        tab[i] = 0;
    }
}

Vecteur::Vecteur(float *tableau, int taille){
    dim = taille;
    tab = new float[dim];
    for(int i=0;i<dim;i++){
        tab[i] = tableau[i];
    }
}

Vecteur::~Vecteur(){
    delete tab;
}

Vecteur::Vecteur(const Vecteur &a){
    dim = a.dim;
    tab = new float[dim];
    for(int i=0;i<dim;i++){
        tab[i] = a.tab[i];
    }
}

Vecteur Vecteur::operator=(const Vecteur &a){
    if(this != &a){
        this->~Vecteur();
        dim = a.dim;
        tab = new float[dim];
        for(int i=0;i<dim;i++){
            tab[i] = a.tab[i];
        }
    }
    return *this;
}

Vecteur Vecteur::operator+(const Vecteur &a){
    if(dim == a.dim){
        float tab_b[dim];
        for(int i=0;i<dim;i++){
            tab_b[i] = tab[i] + a.tab[i];
        }
        Vecteur b(tab_b, dim);
        return b;
    } else {
        cout <<"Les vecteurs ne sont pas de meme taille.\n";
        float tab_b[1] = {0};
        Vecteur b(tab_b, 1);
        return b;
    }
}     

Vecteur Vecteur::operator-(const Vecteur &a){
    if(dim == a.dim){
        float tab_b[dim];
        for(int i=0;i<dim;i++){
            tab_b[i] = tab[i] - a.tab[i];
        }
        Vecteur b(tab_b, dim);
        return b;
    } else {
        cout <<"Les vecteurs ne sont pas de meme taille.\n";
        float tab_b[1] = {0};
        Vecteur b(tab_b, 1);
        return b;
    }
}    

float & Vecteur::operator[](int i){
    return tab[i];
}

Vecteur Vecteur::subvec(int i, int j){
    float sous_tab[j-i+1];
    for(int k=i;k<j+1;k++){
        sous_tab[k-i]=tab[k];
    }
    Vecteur c(sous_tab, j-i+1);
    return c;
}

int Vecteur::getDimension(){
    return dim;
}

Vecteur operator*(float k, const Vecteur &d){
    int dimension = d.dim;
    float tab_b[dimension];
    for(int i=0;i<dimension;i++){
        tab_b[i] = k * d.tab[i];
    }
    Vecteur b(tab_b, dimension);
    return b;
}

Vecteur::Vecteur(){ }


class Matrice{
    private:
        Vecteur *mat;
        int dims[2];

    public:
        void affiche();
        Matrice();
        Matrice(int, int);
        Matrice(Vecteur);
        Matrice(Vecteur *, int);
        ~Matrice();
        Matrice(const Matrice &M);
        Matrice & operator=(const Matrice &);
        Vecteur & operator[](int);
        Matrice operator+(const Matrice &);
        Matrice operator-(const Matrice &);
        Matrice operator*(const Matrice &);
};

void Matrice::affiche(){
    cout << "La matrice est de dimension "<<dims[0]<<" x "<<dims[1]<<endl;
    cout << "Les coefficients de la matrice sont : \n";
    for(int i=0;i<dims[0];i++){
        for(int j=0;j<dims[1];j++){
            cout <<mat[j][i]<<" ";
            if(j==dims[1]-1){
                cout <<"\n";
            }
        }
    }
}

Matrice::Matrice(int nbLignes, int nbColonnes){
    dims[0] = nbLignes;
    dims[1] = nbColonnes;
    mat = new Vecteur[nbColonnes];
    for(int i=0;i<nbColonnes;i++){
        mat[i] = Vecteur(nbLignes);
    }
}

Matrice::Matrice(Vecteur a){
    int taille = a.getDimension();
    dims[0] = taille;
    dims[1] = taille;
    
    mat = new Vecteur[taille];
    for(int i=0;i<taille;i++){
        mat[i] = Vecteur(taille);
    }
    
    //Puis on modifie les termes diagonaux
    for(int i=0;i<taille;i++){
        mat[i][i] = a[i];
    }
}

Matrice::Matrice(Vecteur *M, int taille){
    dims[0] = M[0].getDimension();
    dims[1] = taille;
    mat = new Vecteur[taille];
    for(int i=0;i<dims[1];i++){
        mat[i] = M[i];
    }
}

Matrice::~Matrice(){
    delete mat;
}

//Constructeur de recopie
Matrice::Matrice(const Matrice &M){
    dims[0] = M.dims[0];
    dims[1] = M.dims[1];
    mat = new Vecteur[dims[1]];
    for(int i=0;i<dims[1];i++){
        mat[i] = M.mat[i];
    }
}

Matrice & Matrice::operator=(const Matrice &M){
   if(this != &M)
   {
        this->~Matrice();
        dims[0] = M.dims[0];
        dims[1] = M.dims[1];
        mat = new Vecteur[dims[1]];
        for(int i=0;i<dims[1];i++){
            mat[i] = Vecteur(dims[0]);
        }
        for(int i=0;i<dims[0];i++){
            for(int j=0;j<dims[1];j++){
                mat[j][i] = M.mat[j][i];
            }        
        }
    }
    return *this;
}

Vecteur & Matrice::operator[](int i){
    return mat[i];
}

Matrice::Matrice(){
    mat = nullptr;
}

int main() {
    float tab_a[] = {1,2,3,4};
    Vecteur a(tab_a, 4);
    Matrice A(a);
    float tab_b[] = {5,6,7,8};
    Vecteur b(tab_b, 4);
    Matrice B(b);
    B.affiche();
    B = A;
    B.affiche(); //Show nothing
}

谢谢

最佳答案

这个味道

    this->~Matrice();
    dims[0] = M.dims[0];
    dims[1] = M.dims[1];

您删除了该对象,并再次尝试分配已删除的对象。您无法分配给已删除的对象。

需要注意的是,

dims[0] 

相当于

this->dims[0];

您应该删除调用析构函数的行,并在其析构函数中将对 delete 的调用更改为 delete[]

关于c++ - C++ 中的简单运算符重载 =,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72027845/

相关文章:

c++ - 在 C++ 中按值传递时复制的顺序

c++ - 向下转型时的 static_cast 悬挂引用

c++ - acm问题。我的代码有什么问题?

c++ - 什么时候在 C++ 中使用函数对象?

c++ - 为什么 bind1st 和 bind2nd 需要常量函数对象?

c++ - 如何用 0 memset 匿名 union

c++ - 支持具有固定数组的对象的 BOOST_FUSION_ADAPT_STRUCT?

C++访问 vector 的一定范围的索引

c++ - OpenGL:gluUnProject 的结果不是预期的结果

c++ - 如何传递信号以解除对 Linux 中 pause() 的阻塞?