Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;在数组中保存指针_C++_Arrays_Pointers - Fatal编程技术网

C++ C++;在数组中保存指针

C++ C++;在数组中保存指针,c++,arrays,pointers,C++,Arrays,Pointers,我有一个叫做figGeom的类。类circulo继承自figGeom 我需要创建一个类,允许我将类型为figGeom的对象指针保存在数组中。你能帮我吗 我还想知道如何向数组添加指针或内存地址 注意:我也有rectangle和triangle类,但我删除了这些类,以使文章更短、更可读 我当前的代码给了我一个错误 图OM.h #define TRIANGULO 0 #define RECTANGULO 1 #define CIRCULO 2 class figGeom { double a

我有一个叫做
figGeom
的类。类
circulo
继承自
figGeom

我需要创建一个类,允许我将类型为
figGeom
的对象指针保存在数组中。你能帮我吗

我还想知道如何向数组添加指针或内存地址

注意:我也有
rectangle
triangle
类,但我删除了这些类,以使文章更短、更可读

我当前的代码给了我一个错误

图OM.h

#define TRIANGULO 0
#define RECTANGULO 1
#define CIRCULO 2

class figGeom
{
    double area;
    int tipoFig;

public:
    figGeom();
    figGeom(int);

    void setArea(double);
    double getArea();

    void setTipoFig(int);
    int getTipoFig();

    virtual double calcArea()=0;
    virtual void toString()=0;
};


class circulo:public figGeom
{
    //atributos
    double radio;

public:
    circulo();
    circulo(double);

    void setRadio(double);
    double getRadio();

    double calcArea();
    void toString();
};
图形OM.cpp

#include "figuraGeom.h"
#define _USE_MATH_DEFINES
#include <Math.h>

#include <string>
#include <iostream>

using namespace std;

//FIGGEOM
//Dispositivo
figGeom::figGeom(){}
figGeom::figGeom(int itipoDis){
    setTipoFig(itipoDis);
}

void figGeom::setArea(double iArea){area = iArea;}
double figGeom::getArea(){return area;}

void figGeom::setTipoFig(int iTipoDis){tipoFig = iTipoDis;}
int figGeom::getTipoFig(){return tipoFig;}

//CIRCULO
circulo::circulo(){}
circulo::circulo(double iRadio):figGeom(CIRCULO){setRadio(iRadio);}

void circulo::setRadio(double iRadio){radio = iRadio;}
double circulo::getRadio(){return radio;}

double circulo::calcArea(){return M_PI*pow(getRadio(),2);}
void circulo::toString(){cout << endl << endl << "  Tipo Figura: Circulo" << endl << endl;}


//LISTA FIGURAS
listaFiguras::listaFiguras(){
    *lista = NULL;
   setNumElementos(0);                          
} 

listaFiguras::~listaFiguras(){
    vaciarLista();
}

void listaFiguras::setNumElementos(int iNum){numElementos = iNum;}
int listaFiguras::getNumElementos(){return numElementos;}

void listaFiguras::vaciarLista()
{
    free(lista);
}
#include "figuraGeom.h"

#include <iostream>
using namespace std;

int tipoFig; 
char opcion;

figGeom * dI = NULL;
listaFiguras* listaFig = new listaFiguras();

void menu();
void menu_add_figura();
figGeom* pedirTriangulo();
figGeom* pedirRectangulo();
figGeom* pedirCirculo();

void menu();
void main()
{
    setlocale(LC_ALL, ""); //Configuración Regional

    menu();
}


void menu()
{

    do{
        cout << "SELECCIONA UNA OPCIÓN" << endl;
        cout << " [1]Añadir elemento" << endl;
        cout << " [2]Ver elemento" << endl;
        cout << " [3]Eliminar elemento" << endl;
        cout << " [4]Ver todos los elementos" << endl;
        cout << " [5]Eliminar todos los elementos" << endl << endl;
        cout << " [6]Salir" << endl << endl;
        cout << "Opción: ";
        cin >> opcion;

        switch (opcion){
                    case '1':
                        menu_add_figura();
                        break;
                    case '2': 

                        break;
                    case '3':

                        break;
            }
    }while(opcion != '6');

}


void menu_add_figura()
{
do{
        system("cls"); //limpiamos pantalla
        cout << "¿Qué tipo de figura desea crear?" << endl;
        cout << " [1]Triangulo" << endl;
        cout << " [2]Rectangulo" << endl;
        cout << " [3]Circulo" << endl;
        cout << " [4]Salir" << endl << endl;
        cout << "Figura: ";
        cin >> opcion;
        //PUNTERO AUX
        //listaFiguras-

        int new_numElem = (listaFig->getNumElementos()) + 1;

        listaFig->setNumElementos(new_numElem);

        figGeom** vector = new figGeom*[new_numElem];

        switch (opcion){
                case '1':
                    dI = pedirTriangulo(); //dI
                    *vector[new_numElem-1] = *dI;
                    break;
                case '2': 
                    dI = pedirRectangulo(); //dI
                    *vector[new_numElem-1] = *dI;
                    break;
                case '3':
                    dI = pedirCirculo(); //dI
                    *vector[new_numElem-1] = *dI;
                    break;
        }

        if(opcion != '4')
        {
            //cout << endl << " Area: " << dI->calcArea() << endl << endl; //Mostrar area
            cout << endl << " Area: " << vector[new_numElem-1]->calcArea << endl << endl; //Mostrar area

            system("pause"); //pausa
            system("cls"); //limpiamos pantalla
        }else delete dI;

    }while(opcion != '4');

}

figGeom* pedirCirculo()
{
    int radio;

    cout << " -Radio: ";
    cin >> radio;

    figGeom* dIaux;
    dIaux = new circulo(radio);
    return dIaux;
}
#包括“figuraGeom.h”
#定义使用数学定义
#包括
#包括
#包括
使用名称空间std;
//菲格姆
//性欲
figGeom::figGeom(){}
figGeom::figGeom(int itipoDis){
setTipoFig(itipoDis);
}
void figGeom::setArea(双iArea){area=iArea;}
double figGeom::getArea(){return area;}
void figGeom::setTipoFig(int-iTipoDis){tipoFig=iTipoDis;}
int figGeom::getTipoFig(){return tipoFig;}
//圆形
circulo::circulo(){}
circulo::circulo(双iRadio):figGeom(circulo){setRadio(iRadio);}
void circulo::setRadio(双iRadio){radio=iRadio;}
双圈::getRadio(){return radio;}
双圈::calcArea(){return M_PI*pow(getRadio(),2);}
void circulo::toString(){cout您可以执行以下操作:

figGeom* array[10];// change 10 to any number you need
然后创建指向
circulo
类的指针并将其保存在数组中

for(int i=0;i<10;i++){
  array[i] = new circulo(/*parameters to constructor*/);
}
for(int i=0;i
  • 不要使用原始指针。使用原始指针通常会导致内存泄漏。相反,请使用C++11(或Boost)的各种智能指针类,如
    std::unique_ptr
    std::shared_ptr
    。它们将在不再需要对象时处理删除对象的问题

    #include <memory>
    
    std::shared_ptr<figGeom> createFigure()
    {
        std::shared_ptr<figGeom> thing(new figGeom(/* whatever */));
        return thing;
    }
    
  • 对于这样一个简单、轻量级的类,您根本不需要使用
    new
    ,您可以在堆栈上分配实例,并在需要时将它们复制到容器中

    figGeom createShape()
    {
        figGeom shape(/* whatever */);
    
        return shape;
    }
    
    std::vector<figGeom> createShapes()
    {
        std::vector<figGeom> shapes;
        shapes.push_back(createShape());
    
        return shapes;
    }
    
    figGeom createShape()
    {
    figGeom形状(/*任意*/);
    返回形状;
    }
    std::vector createShapes()
    {
    向量形状;
    shapes.push_back(createShape());
    返回形状;
    }
    

  • 为基类创建一个指针数组并插入派生类对象。由于动态并行性,它将指向派生类。在您的例子中,figGeom*ptr[N];为什么不使用
    std::vector
    并将指针存储在其中?您应该发布错误消息。
    #include <vector>
    
    std::vector<std::unique_ptr<figGeom>> createFigures()
    {
        std::vector<std::unique_ptr<figGeom>> figures;
        figures.push_back(createFigure());
        figures[0].setArea(1234.56);
        return figures;
    }
    
    figGeom createShape()
    {
        figGeom shape(/* whatever */);
    
        return shape;
    }
    
    std::vector<figGeom> createShapes()
    {
        std::vector<figGeom> shapes;
        shapes.push_back(createShape());
    
        return shapes;
    }