C ++结构不起作用

时间:2015-12-28 20:15:42

标签: c++

我有一个问题。

public String toString() {
    String str = "";
    for (int row = 0; row < screen.length; row++) {
        for (int col = 0; col < screen[row].length; col++) {
            if (row == 0 || col == 0 || row == screen.length-1) {
                str += border;
            } else {
                border += screen[row][col];
            }
        }
        str += "\n";
    }
    return str;
}

当我运行它时,我的编译器停止工作。 但如果我改变

        List<System.Windows.Controls.CheckBox> CheckboxList = new List<CheckBox>();

        public List<string> Users = new List<string> { "First Student", "Very First Student", "Second Student", "Student Student" };

        for (int i = 0; i < Users.Count; i++)
        {
            CheckboxList.Add(new System.Windows.Controls.CheckBox());
            heckboxList[i].RenderTransform = new ScaleTransform(1.5,1.5);

            CheckboxList[i].Content = Users[i];
            CheckboxList[i].Margin = new Thickness(5, 5, 5, 0);
            Panel.Children.Add(CheckboxList[i]);             
        }
    }

要:

struct Info{
  string name;
  string lastname;
  int BirthDate[];
  int DeathDate[];
}human[2];
......

    for(int j=0; j < 3; j++){
        ReadFromFile >> human[0].BirthDate[j];
    }
......

一切正常。所以我的问题是有可能以某种方式用数组来做吗?例如,我有2个人,我想从文件中读取他们的BirthDate。我最初不能制作2个变量因为我不知道我的文件中有多少人。

2 个答案:

答案 0 :(得分:3)

不需要BirthDateDeathDate的数组?

另外:您的j计为3。

试试这个:

struct Info{
    string name;
    string lastname;
    int BirthDate;
    int DeathDate;
} human[2];
......

    for(int j=0; j < 2; j++){
        ReadFromFile >> human[j].BirthDate;
    }
......

<强>更新

  

BirthDate包含如下:2015 12 28 in file。

正如Thomas Matthews所说:

struct MyDate {
    unsigned int year;
    unsigned int month;
    unsigned int day;
};

struct Info{
    string name;
    string lastname;
    MyDate BirthDate;
    MyDate DeathDate;
} human[2];
......

    ReadFromFile >> human[0].BirthDate.year;
    ReadFromFile >> human[0].BirthDate.month;
    ReadFromFile >> human[0].BirthDate.day;
......

答案 1 :(得分:2)

做这样的事情:

struct Date{
    int day;
    int month;
    int year;
};
struct Info{
    string name;
    string lastname;
    Date BirthDate;
    Date DeathDate;
}human[2];


ReadFromFile >> human[0].BirthDate.day;
ReadFromFile >> human[0].BirthDate.month;