creating an array of structs in c++

后端 未结 4 1331
谎友^
谎友^ 2021-01-31 18:21

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

int main         


        
4条回答
  •  天涯浪人
    2021-01-31 18:53

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

    #include 
    
    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;
    }
    

提交回复
热议问题