c++ - C++对象数组中的奇怪数字

标签 c++ arrays memory-management

我一直在尝试创建一个包含动态创建的对象数组的类。我重载了运算符 + 以向一组对象(类到类)添加(目前仅此)一个新对象。问题是当我读取数组中的数据时,我得到了各种大数字。这是代码:

#include<iostream.h>

class Figura {
    public: 
        int x, y, poz;
        int tip; //1 = punct ; 2 = dreapta; 3 = dreptunghi
        Figura() { };
        Figura(const Figura&) { };
};

class Grup {
    private:
        int nr_elemente;
        Figura *figuri;
    public:
        int i;
        Grup(int nr_el) {
            nr_elemente = nr_el;
            figuri = new Figura[nr_elemente];
            i = 1;
        }
        ~Grup() {};
        Grup(const Grup&) {};
        int _nr_elemente() {
            return i;
        }

        void adauga_element(Figura fig) {
            if( i <= nr_elemente)
                figuri[i++] = fig;
            else
                cout<<"Grupul a atins numarul maxim de figuri.";
        }

        void afiseaza_elemente() {
            for(int j = 1; j <= i; j++)
                cout<<"Figura nr : "<<j<<"tip: "<<figuri[j].tip<<figuri[j].x<<" "<<figuri[j].y<<" "<<figuri[j].poz;
        }
    friend Grup operator+(const Figura& fig1, const Figura& fig2) {
        return fig1.poz + fig2.poz;
    };
    friend Grup operator+(const Grup& gr1, const Grup& gr2) {};
    void operator+(const Figura& fig);
    friend Grup operator*(const Grup& fig) {};

};

void Grup::operator+(const Figura& fig) {
    Grup::adauga_element(fig);
}

class Punct : public Figura
{
    public: 
        Punct(int poz) {
            Punct::tip = 1;
            Punct::poz = poz;
        }
};

class Segment : public Figura
{
    public:
        Segment(int poz, int x) {
            Segment::tip = 2;
            Segment::poz = poz;
            Segment::x = x;
        }
};

class Dreptunghi : public Figura
{
    public:
        Dreptunghi(int poz, int x, int y) {
            Dreptunghi::tip = 3;
            Dreptunghi::poz = poz;
            Dreptunghi::x = x;
            Dreptunghi::y = y;
        }
};

void main(void) {

    Grup gr(1);
    Punct pct(1);
    Segment sgm(3, 5);

    gr + pct;
    gr + sgm;
    //cout<<gr.i;
    cout<<sgm.x;
    gr.afiseaza_elemente();

}

最佳答案

数组索引从 0 运行至 N - 1 , 当 N是数组大小。以下代码将导致越界数组访问:

void adauga_element(Figura fig) {
    if( i <= nr_elemente)
        figuri[i++] = fig;
    else
        cout<<"Grupul a atins numarul maxim de figuri.";
}

更改为:

    if( i < nr_elemente)

同样的问题在 afiseaza_elemente() .

因为您动态分配了成员,所以析构函数必须 delete[]需要正确实现或声明动态分配的数组以及复制构造函数和赋值运算符 private以防止复制。由于这是 C++,请考虑使用 std::vector<Figura>而不是数组。

关于c++ - C++对象数组中的奇怪数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10288155/

相关文章:

Java:从 JSON 解析数组的数组

Javascript 将数据添加到数组效果不佳

c++ - 将二维数组(指针)分配给对象中的变量以便在 C++ 中访问?

c++ - 大型原始数字类型之间的算术 : can I save the overflow?

c++ - 最小和最大的多维背包

macos - 如何销毁(解除分配)从 nib 文件加载的 View 项的实例

java - 如何在 Java 中找到物理内存大小?

c++ - void* 和 const void* for function parameter -> struct member 的解决方法

php - JSON多维数组使用mysql_fetch_assoc

haskell - 如何在常量内存中获取 make stats