vector与结构一起使用时出现不完整的类型错误

vector与结构一起使用时出现不完整的类型错误

本文介绍了将std :: vector与结构一起使用时出现不完整的类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c ++ STL向量,并且有一个称为 projectileList的结构向量。我试图遍历向量,在迭代时获取和设置strut中的值,但是我的代码拒绝编译,错误为不允许使用不完整的类型。

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:

代码:

ProjectHandeler.h:

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

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

}


推荐答案

这可以很好地进行编译(在此处进行了测试: ):

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.

这篇关于将std :: vector与结构一起使用时出现不完整的类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:07