简体   繁体   English

在另一个结构中初始化结构数组

[英]initialize an array of structs inside another struct

I'm completely stumped. 我完全迷住了。 How do I fill a struct array that is contained inside another struct ? 如何填充另一个struct内包含的struct数组?

I have the two struct s: 我有两个struct

struct employee_stats{
    char emp_name[MAX_NAME_LENGTH];
    double salary;
    int years_employed;
}

struct annual_reviews{
    int year;
    struct employee_stats employee[NO_EMPLOYEES];
}

I declare a pointer to an array of annual_reviews structs 我声明了一个指向annual_reviews结构数组的指针

annual_reviews *sort_records = new annual_reviews[num_years];

I have a vector of strings that read from a text file earlier in the program. 我在程序的前面有一个从文本文件读取的字符串vector I send the employee_record and year vectors to a method to fill the two struct s 我将employee_recordyear向量发送到一种方法来填充两个struct

void DataSort::load_records(vector<string> employee_record, vector<string> year){
    vector<string> line_data;
    string word;

    //split employee_record by \t and into single line string vector
    for(int i = 0; i < employee_record.size(); i++){
         stringstream wordstream(employee_records[i]);
         while(getline(wordstream, word, '\t')){
             if(!word.empty())
                 line.push_back(word);
         }
    }

Now , within the same method, I want to add this data to my struct s: 现在,在同一方法中,我想将此数据添加到struct

    for(int j = 0; j < num_years; j++){
        sort_records[i].year = atoi(year[i].c_str); //year of record
        for(int k = 0; k < NO_EMPLOYEES; k++){
            //here is where it all falls apart
            sort_records[j].employee_stats[j].emp_name = line[j]; //fill the records
        }
    }
}

I realize I'm not accessing the inside struct correctly but I'm stuck, I've hit a wall. 我意识到我无法正确访问内部struct ,但被卡住了,碰到了墙。

I get two compilation errors using VS2015: 我在使用VS2015时遇到两个编译错误:

std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str':non-standard syntax;use'&'to create a pointer to member

array type 'char[25]' is not assignable

Can someone point me in the right direction? 有人可以指出我正确的方向吗? Do I need to create a pointer to the member struct ? 我需要创建一个指向成员struct的指针吗? I thought you could index the members directly. 我以为您可以直接索引成员。

Thanks. 谢谢。

struct used for making datatype. 用于生成数据类型的结构。

struct employee_stats{
    char emp_name[MAX_NAME_LENGTH];
    double salary;
    int years_employed;
}

struct annual_reviews{
    int year;
    struct employee_stats[NO_EMPLOYEES];
}

on the annual_reviews datatype, i think you declare another datatype by using 在annual_reviews数据类型上,我认为您通过使用声明了另一个数据类型

struct employee_stats

so i think you should declare it as a data array as you want it on the second one 所以我认为您应该在第二个数组中将其声明为数据数组

struct employee_stats{
    char emp_name[MAX_NAME_LENGTH];
    double salary;
    int years_employed;
}

struct annual_reviews{
    int year;
    employee_stats employeeData[NO_EMPLOYEEs];
}

Here after making variable type annual_reviews, i think you can access the employeeData array. 在这里,在创建了变量类型annual_reviews之后,我认为您可以访问employeeData数组。 And then for the char error, i think you should use string as dataType on emp_name. 然后对于char错误,我认为您应该在emp_name上使用字符串作为dataType。

[EDIT] copy string to char [编辑]将字符串复制到char

strcpy(sort_records[j].employee_stats[j].emp_name, line[j].c_str());

Reading source : http://www.cplusplus.com/reference/string/string/c_str/ 阅读源: http : //www.cplusplus.com/reference/string/string/c_str/

You problem is that you are not declaring the inner structure correctly. 您的问题是您没有正确声明内部结构。

You code should look like some thing like this (pseudo-code): 您的代码应该看起来像这样(伪代码):

typedef employee_stats some_type;
Use some_type inside annual_reviews.

The rest is trivial. 其余的都是微不足道的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM