c++ - 动态结构数组错误

标签 c++ arrays struct dynamic-arrays

当我尝试创建 ContestantInfo *contestantStructArray = new ContestantInfo [numberOfContestants];

时出现错误“不完整类型的分配”

这是我的代码:

#include <fstream>
#include <iostream>

using namespace std;

struct ContestantInfo;

int main()
{
    //opens all the files for input and output
    fstream contestantsFile("contestants.txt", ios::in);
    fstream answerKeyFile("answers.txt", ios::in);
    fstream reportFile("report.txt", ios::out);

    //used to determine how many contestants are in the file
    int numberOfContestants = 0;
    string temp;

    //checks to see if the files opened correctly
    if(contestantsFile.is_open() && answerKeyFile.is_open() && reportFile.is_open()){

        //counts the number of lines in contestants.txt
        while(getline(contestantsFile, temp, '\n')){

            numberOfContestants++;

        }

        //Puts the read point of the file back at the beginning
        contestantsFile.clear();
        contestantsFile.seekg(0, contestantsFile.beg);

        //dynamic array that holds all the contestants ids
        string *contestantIDNumbers = new string [numberOfContestants];

        //Reads from the contestants file and initilise the array
        for(int i = 0; i < numberOfContestants; i++){

            getline(contestantsFile, temp, ' ');

            *(contestantIDNumbers + i) = temp;

            //skips the read point to the next id
            contestantsFile.ignore(256, '\n');

        }

        ContestantInfo *contestantStructArray = new ContestantInfo [numberOfContestants];

    }
    else
    {
        cout << "ERROR could not open file!" << endl;
        return 0;
    }

}

struct ContestantInfo{

    string ID;
    float score;
    char *contestantAnswers;
    int *questionsMissed;

};

Struct ContestantInfo 中的指针最终也应该指向动态数组,如果发生任何变化的话。我是一名学生,所以如果我在做一些愚蠢的事情,请不要犹豫。

最佳答案

根据编译器,您的问题是结构的前向声明(当您尝试创建它们的数组时)。查看此问题及其答案:Forward declaration of struct

问候

关于c++ - 动态结构数组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46600042/

相关文章:

c++ - 脚本 - 值类型 - 如何解决这个问题?

c++ - CMake 无法在 Clion 的 Windows 上找到 freeglut

c++ - 通过引用将变量传递给另一个类

php - 如何使用 symfony dom 爬虫将 html 表解析为数组

javascript - 使用 Javascript 在 1 个数组中查找公共(public)元素

python - 在numpy中随机排列数组的一部分

c++ - 合并/加入两个结构

ios - 面向协议(protocol)的编程示例在 swift 3.0 中不起作用

c - 全局结构体指针初始化

c++ - GCC 交叉编译器 Cygwin64 : GFDL license tm. texi