0

I'm doing a dinamic int vectors class in C++ for a school project. I have a problem with the "+" operator overloading that's meant to concatenate both vectors. I'm trying to run this code in the main.cpp:

#include <iostream>
#include "..\vectoresDinamicos\VectorEnteros.h"

using namespace std;

int main(){
    int ve1[] = {1,2,3,4,5,6,7,8};
    int ve2[] = {9,10,11,12,13};
    VectorEnteros v1;
    VectorEnteros v2(ve2,sizeof(ve2)/sizeof(ve2[0]));
    VectorEnteros v3;
    cout << v1.agregar(ve1,sizeof(ve1)/sizeof(ve1[0]))<<endl;

    v3 = v1+v2;
    //v3 = v3+14;
    cout << v3 << endl;
    return 0;
}

and the Class is defined as :

#include "VectorEnteros.h"

#define TAM_INT (sizeof(int))

VectorEnteros::VectorEnteros(){
    cantElem=0;
    vec=NULL;
}
VectorEnteros::~VectorEnteros(){
    delete [] vec;
}
VectorEnteros::VectorEnteros(int* vec,unsigned cantElem){

    this->vec = new int[cantElem];
    memcpy(this->vec,vec,cantElem*TAM_INT);
    this->cantElem=cantElem;
}
VectorEnteros::VectorEnteros(const VectorEnteros& vec1,const VectorEnteros vec2){
    this->vec = new int[vec1.cantElem + vec2.cantElem];
    memcpy(this->vec,vec1.vec,vec1.cantElem*TAM_INT);
    memcpy(this->vec+vec1.cantElem*TAM_INT,vec2.vec,vec2.cantElem*TAM_INT);
    this->cantElem = vec1.cantElem+vec2.cantElem;
}

VectorEnteros& VectorEnteros::agregar(int* vec, unsigned cantElem){

    this->vec = (int*)realloc(this->vec,cantElem*TAM_INT + this->cantElem*TAM_INT);
    memcpy(this->vec+this->cantElem*TAM_INT,vec,cantElem*TAM_INT);
    this->cantElem +=cantElem;

    return *this;
}

VectorEnteros VectorEnteros::operator +(const VectorEnteros& vec2){
    return VectorEnteros(*this,vec2);
}

VectorEnteros VectorEnteros::operator +(const int num){
    int vectorAux[this->cantElem + 1];
    memcpy(&vectorAux[0], this->vec, this->cantElem*TAM_INT);
    memcpy(&vectorAux[this->cantElem], &num, 1);
    return VectorEnteros(vectorAux,this->cantElem+1);
}

ostream& operator <<(ostream& os,const VectorEnteros& vec){ // friend operator
    unsigned i;
    os << '[';
    for(i=0;i<vec.cantElem-1;i++){
        os << (*(vec.vec+i))<<", ";
    }
    os << (*(vec.vec+i))<<"]";
    return os;
}

And the headers file is this (Sry for no putting it in the first place)

#ifndef VECTORENTEROS_H
#define VECTORENTEROS_H

#include <iostream>

using namespace std;

class VectorEnteros
{
    public:
        VectorEnteros();
        ~VectorEnteros();
        VectorEnteros(const VectorEnteros& vec1,const VectorEnteros vec2);
        VectorEnteros(int* vec,unsigned cantElem);
        
        VectorEnteros& agregar(int* vec, unsigned cantElem);
        VectorEnteros operator +(const VectorEnteros& vec2);
        VectorEnteros operator +(const int num);

        friend ostream& operator <<(ostream& os,const VectorEnteros& vec2);
    protected:

    private:
        int* vec;
        unsigned cantElem; 
};

I don't really understand but by using a dbg i understand that maybe the destructor is called after the "+" operator and the object that's meant to go into "v3" and it's inner vectors are destroyed, so when i call the "<<" operator it's accessing freed memory so it crashes.

Does anyone have any explanation of what's really happening? (Sorry for the big chunck of code!!!)

  • To make this a proper [mcve] you need the class declaration posted as well (the header). This has the pungent aroma of a failure to comply with [Rule of 3/5/0](https://en.cppreference.com/w/cpp/language/rule_of_three). – WhozCraig Jun 25 '22 at 16:21
  • Your operator+ is return-by-value operator. – Jean-Baptiste Yunès Jun 25 '22 at 16:22
  • About the duplicate - you violated the Rule of Three. When you define destructor, you almost surely need to also define copy constructor and copy assignment operator. In this case, `v3 = v1 + v2` calls compiler-defined `operator=(const VectorEnteros&)`, which does a shallow copy. You need to provide your own implementation that will perform a deep copy instead. – Yksisarvinen Jun 25 '22 at 16:23

0 Answers0