0

I'm getting the error "Allocation of incomplete type" when I try to create ContestantInfo *contestantStructArray = new ContestantInfo [numberOfContestants];

Heres my code:

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

};

The pointers inside Struct ContestantInfo are eventually supposed to point to dynamic arrays as well if that changes anything. I'm a student so don't hold back if I'm doing something stupid.

2 Answers 2

1

According to the compiler, your problem is the forward declaration of the struct (as you try to create an array of them). See this question and its answer: Forward declaration of struct

Regards

Sign up to request clarification or add additional context in comments.

2 Comments

Alright I roughly understand that post. Can you suggest an inclusion of code that would fix this or does creating a dynamic struct array like this just not work?
@FinnWilliams Sure, you just need to put the struct definition before the main.
0

Is there any reason you need to use pointers?

It would make things more straightforward for you if you use std vectors instead of dynamic array allocation with new.

In your structure you could have a vector if ints and a vector of strings instead of a pointer to a char.

You could also have a vector of contestant infos.

You then don't need to worry about resource management and can let the standard template library handle it.

See here for more info:

http://www.cplusplus.com/reference/vector/vector/

1 Comment

Yes the assignment restricts us to only us dynamic arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.