结构中带有数组的C ++动态内存分配 [英] C++ dynamic memory allocation with arrays in a structure

查看:76
本文介绍了结构中带有数组的C ++动态内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也有类似的问题,但是它们都是用C而不是C ++编写的,所以我问了一个新问题.

There have been similar questions to this but they are all in C, rather than C++, so I have asked a new question.

我一直在遵循 C ++教程,并在完成了动态内存之后,找到了指针和结构部分,我试图将它们放在示例程序中.

I have been following a C++ tutorial and after completing the dynamic memory, pointers and structure sections I tried to put them together in an example program.

本质上,我试图动态分配一个结构数组(程序输入"produce":P并显示结果).

Essentially, I am trying to have a dynamically allocated array of a structure (the program inputs "produce" :P and displays the result).

编译器错误:代码fruit[i]->item;

很抱歉,如果代码有些冗长(我不想在出现问题的情况下省去部分内容,即使这会导致问题过于本地化"):

Sorry if the code is a bit long winded (I didn't want to leave out sections in case they were the problem, even if this results in the question being 'too localised'):

#include <iostream>
#include <string>
#include <new>

using namespace std;

struct produce {
    int price;
    string item;
};

int main(void) {
    int num;
    int i;

    //Get int for size of array
    cout << "Enter the number of fruit to input: ";
    cin >> num;
    cout << endl;

    //Create a dynamically allocated array (size num) from the produce structure
    produce *fruit = new (nothrow) produce[num];
    if (fruit == 0) {
        cout << "Error assigning memory.";
    }
    else {
        //For 'num', input items
        for (i = 0; i < num; i++) {
            cout << "Enter produce name: ";
            //Compiler error: 'base operand of '->' has non-pointer type 'produce'
            cin >> fruit[i]->item;
            cout << endl;

            cout << "Enter produce price: ";
            cin >> fruit[i]->price;
            cout << endl;

            cout << endl;
        }
        //Display result
        for (i = 0; i < num; i++) {
            cout << "Item: " << fruit[i]->item << endl;
            cout << "Cost: " << fruit[i]->price << endl;
            cout << endl;
        }
        //Delete fruit to free memory
        delete[] fruit;    
    }

    return 0;
}

推荐答案

请考虑以下简单示例,使您可以清楚地知道如何访问对象而不是对象的指针:

Consider this simple example to make it explicit how you are accessing an object, not a pointer to an object:

int *arr = new (std::nothrow) int[10];
for(int i=0; i< 10 ; ++i)
{
    arr[i]=i;
}
delete [] arr;

arr(例如arr [0])中的每个元素都是一个简单的int,为便于示例,它正在使用索引值初始化数组中每个元素的内容,随后删除int数组.对于您而言,水果数组中的每个元素(fruit [0],fruit [1]等)都是类型为Produce的对象(而不是指向该对象的指针).因此,访问权限必须是访问权限操作员.而不是-> .

Each element in arr( for instance arr[0]) is a simple int, for the sake of the example it is being initialized the content of each element of the array with the index value, later the array of ints is deleted. For you case each element in the fruit array (fruit[0], fruit[1], etc...) is an object of type produce (not a pointer to an object). Therefore the access must be with the access operator . instead of ->.

这篇关于结构中带有数组的C ++动态内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆