C++ 动态结构数组错误
当我尝试创建参赛者信息*参赛者结构数组=新参赛者信息[NumberOfCompetitors]时,我得到了不完整类型的错误分配 这是我的密码:C++ 动态结构数组错误,c++,arrays,struct,dynamic-arrays,C++,Arrays,Struct,Dynamic Arrays,当我尝试创建参赛者信息*参赛者结构数组=新参赛者信息[NumberOfCompetitors]时,我得到了不完整类型的错误分配 这是我的密码: #include <fstream> #include <iostream> using namespace std; struct ContestantInfo; int main() { //opens all the files for input and output fstream contesta
#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 Competitive Info中的指针最终也应该指向动态数组,如果这改变了什么。我是一名学生,所以如果我在做傻事,不要退缩。你有什么理由需要使用指针吗 如果您使用std向量而不是使用新的动态数组分配,这将使事情变得更加简单 在您的结构中,可以有一个向量if int和一个字符串向量,而不是一个指向字符的指针 你也可以有一个参赛者信息的载体 这样,您就不必担心资源管理,可以让标准模板库来处理它 有关更多信息,请参见此处:
根据编译器的说法,您的问题是在尝试创建结构数组时,结构的正向声明。请参见此问题及其答案:
关于是的,任务限制我们只使用动态数组。好的,我大致理解这篇文章。你能建议包含一些代码来解决这个问题吗?或者像这样创建一个动态结构数组是行不通的吗?@FinnWilliams当然,你只需要把结构定义放在主数组之前。