【问题标题】:Array of objects dynamic memory allocation对象数组动态内存分配
【发布时间】:2012-05-04 09:06:56
【问题描述】:

我正在尝试为对象数组分配动态内存,但我不断收到以下错误:

“错误:C2143:语法错误:缺少';'前 '*'” “错误:C2501:'Figura':缺少存储类或类型说明符”

欢迎任何帮助。我正在使用 Visual Basic C++ 2006,但将切换到带有 Dosbox 的 Turbo C++ 3.0 以使 graphics.h 工作:( .

代码如下:

#include<iostream.h>

class Grup {
    private:
        int nr_elemente;
        Figura *figuri;
    public:
        Grup() {
            figuri = new Figura[nr_elemente];
        }
};


class Figura {

public:
    Figura();
    ~Figura();
    Figura(const Figura&);

    friend Figura operator+(const Figura& fig1, const Figura& fig2) {};
    friend Figura operator+(const Grup& gr1, const Grup& gr2) {}

    friend Figura operator*(const Figura& fig) {}
    friend Figura operator*(const Figura& fig) {}

};

Figura operator+(const Figura& fig, const Grup& gr) {
    return fig;
}

class Punct : Figura
{
public:
    int x, y;

    Punct(int x, int y) {
        Punct::x = x;
        Punct::y = y;
    }
};

class Segment : Figura
{
public:
    int x, y, poz;
};

class Dreptunghi : Figura
{
public:
    int x, y, poz;
};

void main(void) {



}

【问题讨论】:

  • Turbo C++ 3.0 来自 1991 年。不推荐用于任何用途。

标签: c++ arrays class memory-management


【解决方案1】:

在这一行:

figuri = new Figura[nr_elemente];

编译器不知道类Figura 存在。因此,它会产生一个错误,因为此时“Figura”是一个未知的标记。您应该使用前向声明:

class Figura; // forward-declare class Figura

class Grup {
/* ... */
};


class Figura {
/* ... */
};

问题是编译器不知道类Figura 的大小,因此它不能分配这种类型的对象数组。因此,您可能需要使用指针或修改您的类设计。

【讨论】:

  • 你能给我举个例子,说明如何使用指针代替我现在正在做的事情吗?
  • 他不需要完整的 Figura 类来调用 new to work 吗?
  • 好的,我前向声明了 Figura,但现在“没有合适的默认构造函数可用”。在 figuri = new Figura[nr_elemente];
  • @Bdesign 您需要声明和在Grup 之前定义Figura,正如我在回答中所说的那样......
  • 嗯,我想我应该考虑重新设计一下类。
【解决方案2】:

您的问题是您试图在一个文件中完成所有操作。如果将类的声明和定义移动到单独的.hpp 和.cpp 文件中,就不会出现此类问题。 移动

// file grup.hpp

class Figura; // forward declaration

class Grup {
private:
    int nr_elemente;
    Figura *figuri;
public:
    Grup();
};

到grup.hpp 文件。然后在group.cpp文件中实现Grup的构造函数:

// file grup.cpp
#include "grup.hpp"
#include "figura.hpp"


Grup::Grup()
{
    figuri = new Figura[nr_elemente];
}

还将 Figura 类的定义和声明移动到对应的 figura.hpp 和 figura.cpp 文件中。最好对所有类使用相同的方法。

【讨论】:

    【解决方案3】:

    您需要转发声明,已经提到,但这还不够,您需要知道类的大小才能将其用作实现。所以……

    class Figura;
    class Grup{
      Figura * figuri;
      //.. other stuff, but ONLY declarations no implementations.
    };
    
    class Figura
    {
      //Same as you already have...
    };
    

    在声明类之后,由于它们在同一个文件中,因此您需要在其他类已经实现(或声明)之后实现。

    //Still same file but below the Figura class definition..
    Grup::Grup() {
       //Now you can use Figura...
       figuri = new Figura[nr_elemente];
    }  
    

    @be 您在下面的评论中看到的问题与您的 fig 类的赋值运算符有关。

    操作员看起来像这样... Fig& operator=(const Fig& );

    但是在您的示例中,您正在分配一个指针 无效函数(常量图 * 图) { //... 无花果容器[索引] = 无花果; // 如果你做了 *fig;你会没事的 //...
    }

    【讨论】:

    • 我做了一些重新设计,现在几乎一切正常。唯一的问题是我认为我没有正确进行动态内存分配。我得到:错误 C2679:二进制 '=':未定义运算符,它采用 'class Figura *' 类型的右手操作数。该行是: void adauga_element(Figura *fig) { figuri[nr_elemente++] = fig; } .
    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2017-05-13
    • 2010-09-20
    • 1970-01-01
    • 2019-03-01
    相关资源
    最近更新 更多