Incomplete type error when using std::vector with

2019-09-19 13:30发布

问题:

I'm working with c++ STL vectors, and have a vector of structures called projectileList. I'm trying to iterate through the vector, getting and setting values in the struts as I iterate, but my code refuses to compile, with the error 'Incomplete type is not allowed.'

Can anyone please point out what I'm doing wrong:

Code:

ProjectHandeler.h:

#include "stdafx.h"
#include "DataTypes.h"
#include <vector>

class ProjectileHandeler {

private:

    int activeObjects;

    std::vector<projectile> projectileList;

    void projectileUpdater();


public:
    ProjectileHandeler(projectile* input[], int projectileCount);

    ~ProjectileHandeler();

};

#endif

projectileHandeler.cpp

#include "stdafx.h"
#include "DataTypes.h"
#include "ProjectHandeler.h"
#include <vector> 

ProjectileHandeler::ProjectileHandeler(projectile* input[], int projectileCount)
{
    for (int i = 0; i < projectileCount; i++)
    {
        projectileList.push_back(*input[i]);
        activeObjects += 1;
    }

    //NO extra slots. Not that expensive.
    projectileList.resize(projectileList.size());
}

void ProjectileHandeler::projectileUpdater()
{
    while (true)
    {
        for (unsigned int i = 0; i < projectileList.size(); i++)
        {
            if (projectileList[i].isEditing == true)
                break;
        }
    }

}

回答1:

This compiles fine (tested it here: http://codepad.org/cWn6MPJq):

#include <vector>

struct projectile {
    bool isEditing;

    };


class ProjectileHandeler {

private:
    std::vector<projectile> projectileList;

void projectileUpdater()
{
    //This bit loops to infinity and beyond! ...or at least untill the handeler is destroyed.
    while (true)
    {
        for (unsigned int i = 0; i < projectileList.size(); i++)
        {
            if (projectileList[i].isEditing == true) //Throws Incomplete type error
                break;
        }
    }

}
};

int main()
{
}

Notice the removal of *, correct type of loop variable and removal of extra class specifier.