【问题标题】:Dynamically Allocated Array of Structures: Assignment Issues (C++)动态分配的结构数组:赋值问题 (C++)
【发布时间】:2014-08-05 05:49:58
【问题描述】:

这是这组的最后一个问题。我需要创建一个动态分配的结构数组,然后我必须访问这些结构中的数据才能插入到输出流中。问题是,我使用的编译器 (g++) 不接受我为数组中的结构赋值的方式。代码如下:

#include <iostream>
#include <cstring>
using namespace std;

struct candy
{
        string name;
        float weight;
        int cal;
};

int main()
{
        candy * pc = new candy [3];
        pc[0] = {"ChocoBar", 4.5, 230};
        pc[1] = {"SugarCrack", 9.3, 690};
        pc[2] = {"TamponBar", 1.3, 100};

        cout << "Bar None:\n";
        cout << "Name: " << pc[0].name << endl;
        cout << "Weight: " << pc[0].weight << endl;
        cout << "Calories: " << pc[0].cal << "\n\n";
        cout << "Bar One:\n";
        cout << "Name: " << pc[1].name << endl;
        cout << "Weight: " << pc[1].weight << endl;
        cout << "Calories: " << pc[1].cal << "\n\n";
        cout << "Bar Two:\n";
        cout << "Name: " << pc[2].name << endl;
        cout << "Weight: " << pc[2].weight << endl;
        cout << "Calories: " << pc[2].cal << "\n\n";

        delete [] pc;
        return 0;
}

已经定义了结构类型——candy;并创建了一个指针(pc)来保存 new 为三个结构分配的内存中的地址,然后我尝试为这三个结构分配值。然而,编译器吐出一条消息说“扩展初始化列表不可用......”,这告诉我我把代码搞砸了,以至于编译器甚至不会将我的结构类型识别为结构(否则它会接受我的三个值列表)。
我今天刚刚学习了数组、结构、指针和变量的动态分配,当涉及到静态分配的结构数组和动态分配的结构和数组(分别)时,我第一次尝试就完成了练习;但是动态分配的结构数组给我带来了极大的痛苦。 请帮忙。

【问题讨论】:

  • 您的代码没有任何问题(请参阅它在 GCC 4.8.1 下运行 here),但它确实使用了 C++11 功能 - 您确定您在编译时启用了这些功能吗?即编译器命令行上的-std=c++11?你有足够新的 GCC 版本吗?
  • @TonyD 它是偶然编译的(没有&lt;string&gt; 标头。)
  • 当您使用数组如:pc[0] 时,您将引用数组中的第一个元素,在您的情况下,它恰好是一个糖果对象。所以 pc[0] 是对糖果的引用,而不是对内存块的引用。我建议您首先初始化您的糖果对象,然后将其添加到数组的指针中。您可能需要执行以下操作: pc[0].name = "ChocoBar"; pc[0].weight = 4.5; pc[0].cal = 230;然后打印你需要: cout
  • @Juniar 他们想做的很好。它只需要 C++11。

标签: c++ arrays


【解决方案1】:

首先,您需要为std::string 包含&lt;string&gt; 标头。其次,您需要确保您的编译器支持 C++11。这将使以下代码合法:

#include <iostream>
#include <string>    // for std::string

struct candy
{
  std::string name;
  float weight;
  int cal;
};

int main()
{
  candy* pc = new candy[3];
  pc[0] = {"ChocoBar", 4.5, 230}; // requires C++11
  delete [] pc;
}

接下来,您可以了解std::vector,这是一个为您进行动态内存分配/解除分配的类模板,并且可以调整其存储大小,就像一个可以增长的数组一样有效:

#include <iostream>
#include <string>
#include <vector>

struct candy
{
  std::string name;
  float weight;
  int cal;
};

int main()
{
  using std::cout;
  using std::endl;
  using std::vector;

  vector<candy> pc;
  pc.push_back({"ChocoBar", 4.5, 230});
  pc.push_back({"SugarCrack", 9.3, 690});

  for (size_t i = 0; i < pc.size(); ++i)
  {
      cout << "Name: " << pc[i].name << endl;
  }
}

请注意,在 C++11 中,您还可以使用一组元素初始化向量:

std::vector<candy> candies{{"ChocoBar", 4.5, 230},
                           {"SugarCrack", 9.3, 690},
                           {"TamponBar", 1.3, 100}};

【讨论】:

  • 那很快。谢谢。
  • 好的,安装g++-4.9,完全更新升级了我的操作系统;但是编译器仍然会发出相同的错误消息。知道有什么问题吗?
  • @user3179580 使用标志-std=c++11
猜你喜欢
  • 2022-11-17
  • 1970-01-01
  • 2012-12-22
  • 2014-08-22
  • 1970-01-01
  • 2017-05-07
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
相关资源
最近更新 更多