0
#include "C_IntArray.h"

C_IntArray::C_IntArray()
{
    m_Array = 0;
    m_Length = 0;
}

C_IntArray::~C_IntArray(void)
{
    delete m_Array;
    m_Length = 0;
}

void C_IntArray::ContructorWithParater(int *intArray, int size)
{
    m_Array = new int[size];
    m_Length = size;
    for (int i = 0; i < size; i++)
        m_Array[i] = intArray[i];
}

void C_IntArray::InputArray()
{
    cout << "Nhap so luong phan tu: ";
    cin >> m_Length;

    m_Array = new int [m_Length];

    for(int i = 0; i < m_Length; i++)
    {
        cout << "Nhap phan tu Array[" << i << "] = ";
        cin >> m_Array[i];
    }
}

void C_IntArray::OutputArray()
{
    for(int i = 0; i < m_Length; i++)
        cout << m_Array[i] << " ";
}

C_IntArray C_IntArray::Remove(int x)
{
    C_IntArray temp;
    temp.ContructorWithParater(m_Array, m_Length);
    temp.OutputArray();
    for(int i = 0; i < temp.m_Length; i++)
    {
        if(temp.m_Array[i] == x)
        {

            {
                temp.m_Length--;
                for(int j = i; j < temp.m_Length; j++)
                    temp.m_Array[j] = temp.m_Array[j + 1];
            }
        }
        cout << "\n";
        temp.OutputArray();
    }
    cout << "\n";
    return temp;
}

File Header

#include <iostream>
using namespace std;

#ifndef _C_IntArray_h
#define _C_IntArray_h

class C_IntArray
{
private:
    int *m_Array, m_Length;

public:
    C_IntArray();
    ~C_IntArray();
    // khoi tao tham so dau vao
    void ContructorWithParater(int *, int);

    void InputArray();
    void OutputArray();

    // xoa phan tu trung
    C_IntArray Remove(int );
};

#endif _C_IntArray_h;

File main

#include "C_IntArray.h"

void main()
{
    C_IntArray a;
    a.InputArray();
    int giaTriCanXoa = 5;
    C_IntArray b = a.Remove(giaTriCanXoa);
    b.OutputArray();
    cout << "\n";
    a.OutputArray();
    system("pause");
}

i have tried to debug my project. the function Remove in class is work, and when i'm debug to return temp it still work, but i'm debug next it return NULL or return 1 array function Remove in class can't return temp.

if i remove destructor or my temp is static C_IntArray, my project can run.

if I have misspelled the desire to help people fix. thank you for the attention.

NhoxShockQ8
  • 73
  • 1
  • 5

1 Answers1

0

Remove returns a copy of the class, not a pointer. Therefore it is a copy. Since you do not define a copy constructor or an assignment operator then you'll be using the default copy/assign - which will result in m_Array being deleted more than once.

You can either perform a deep copy of the internal array when copying the class or use Copy on Write and reference counting.

i.e. you will need to add the following functions:

C_IntArray(C_IntArray const& other);
C_IntArray& operator=(C_IntArray const& rhs);

They should allocate new storage for the data in the array and copy the elements from 'other' or 'rhs'. Look up how to write a copy constructor or assignment operator. There will be countless examples online.

There are also memory leaks in your class. C_IntArray::InputArray() will leak memory since you do not delete m_Array before assigning new memory to it.

It would be better to use a free function for input duties rather than making it a class member - keep the interface of your class minimal and complete.

i.e. move it out of the class:

void InputArray(C_IntArray& dst) {
    // ...
}

As others have suggested, just use std::vector.

Pete
  • 4,784
  • 26
  • 33
  • By the way, you should also be using array delete - i.e. delete[] m_Array. Just noticed that one. – Pete Sep 30 '14 at 07:45