在 C++ 中创建一个结构数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6810656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:42:09  来源:igfitidea点击:

creating an array of structs in c++

c++arraysdata-structures

提问by Jason

I'm trying to create an array of structs. Is the code below valid? I keep getting an expected primary-expression before '{'token error.

我正在尝试创建一个结构数组。下面的代码有效吗?我不断收到expected primary-expression before '{'令牌错误。

int main() {
  int pause;
  struct Customer {
    int uid;
    string name;
  };

  Customer customerRecords[2];
  customerRecords[0] = {25, "Bob Jones"};
  customerRecords[1] = {26, "Jim Smith"};
  cin >> pause;
  return 0;
}

回答by Tuomas Pelkonen

Try this:

尝试这个:

Customer customerRecords[2] = {{25, "Bob Jones"},
                               {26, "Jim Smith"}};

回答by Jason

You can't use an initialization-list for a structafter it's been initialized. You've already default-initialized the two Customerstructs when you declared the array customerRecords. Therefore you're going to have either use member-access syntax to set the value of the non-static data members, initialize the structs using a list of initialization lists when you declare the array itself, or you can create a constructor for your struct and use the default operator=member function to initialize the array members.

初始化struct后不能使用初始化列表。Customer当您声明 array 时,您已经默认初始化了这两个结构customerRecords。因此,您要么使用成员访问语法来设置非静态数据成员的值,要么在声明数组本身时使用初始化列表列表来初始化结构,或者您可以为结构创建构造函数并使用默认operator=成员函数来初始化数组成员。

So either of the following could work:

因此,以下任一方法都可以工作:

Customer customerRecords[2];
customerRecords[0].uid = 25;
customerRecords[0].name = "Bob Jones";
customerRecords[1].uid = 25;
customerRecords[1].namem = "Jim Smith";

Or if you defined a constructor for your struct like:

或者,如果您为结构定义了一个构造函数,例如:

Customer::Customer(int id, string input_name): uid(id), name(input_name) {}

You could then do:

然后你可以这样做:

Customer customerRecords[2];
customerRecords[0] = Customer(25, "Bob Jones");
customerRecords[1] = Customer(26, "Jim Smith");

Or you could do the sequence of initialization lists that Tuomas used in his answer. The reason his initialization-list syntax works is because you're actually initializing the Customerstructs at the time of the declaration of the array, rather than allowing the structs to be default-initialized which takes place whenever you declare an aggregate data-structure like an array.

或者您可以执行 Tuomas 在他的回答中使用的初始化列表序列。他的初始化列表语法起作用的原因是因为您实际上Customer是在声明数组时初始化结构,而不是允许结构被默认初始化,这在您声明聚合数据结构时发生大批。

回答by Dave

Some compilers support compound literals as an extention, allowing this construct:

一些编译器支持复合文字作为扩展,允许这种结构:

Customer customerRecords[2];
customerRecords[0] = (Customer){25, "Bob Jones"};
customerRecords[1] = (Customer){26, "Jim Smith"};

But it's rather unportable.

但它相当不便携。

回答by Mato

It works perfectly. I have gcc compiler C++11 ready. Try this and you'll see:

它完美地工作。我已经准备好了 gcc 编译器 C++11。试试这个,你会看到:

#include <iostream>

using namespace std;

int main()
{
    int pause;

    struct Customer
    {
           int uid;
           string name;
    };

    Customer customerRecords[2];
    customerRecords[0] = {25, "Bob Jones"};
    customerRecords[1] = {26, "Jim Smith"};
    cout << customerRecords[0].uid << " " << customerRecords[0].name << endl;
    cout << customerRecords[1].uid << " " << customerRecords[1].name << endl;
    cin >> pause;
return 0;
}