错误C2512在没有显式构造函数的结构中

时间:2013-08-15 22:06:54

标签: c++

我收到错误C2512:'LoadingWorldlet':当我尝试编译此文件时,没有合适的默认构造函数可用。没有明确的构造函数,所以我想不出我接受这个错误的原因。

struct Worldlet {
    int x, z;
    glm::mat4 worldletMatrix;
    std::vector<Voxel> blocks;
};

struct LoadingWorldlet {
    int x, z;
    std::future<Worldlet> &result;
};

class World {
public:
    World();
    ~World();

    void Init();
    void InitRenderable();
    void UpdateWorldletList(int x, int z);

    void Render(Shader* worldShader, Camera *mainPov);

    static Worldlet LoadWorldlet(int x, int z, std::ifstream &file);
private:
    std::vector<Worldlet> worldlets;
    std::vector<LoadingWorldlet> loadingWorldlets;
    std::vector<std::string> loadingTitles;
    std::vector<int> toRemove;

    Renderable cube;

    std::string worldName, prefix;

    static const float CUBE_SIZE;
    static const int LOADLIMIT = 1;

    int GetLoadRadius(int r = 0) {
        static int i = r;
        return i;
    }
};

这是使用LoadingWorldlet的唯一功能:

void World::UpdateWorldletList(int x, int z) {
    static int previousX, previousZ;

    for(int index: toRemove) {
        worldlets.erase(worldlets.begin() + index);
    }
    toRemove.clear();

    int loaded = 0;

    std::vector<int> clearList;
    for(auto &loading: loadingWorldlets) {
        if(loaded >= LOADLIMIT) break;

        worldlets.push_back(loading.result.get());
        clearList.push_back(loaded);
        loaded++;
    }
    for(int i: clearList)
        loadingWorldlets.erase(loadingWorldlets.begin()+i);

    if(previousX != x && previousZ != z) {
        int i = 0;
        for(auto worldlet: worldlets) {
            if(pow(worldlet.x - x, 2) + pow(worldlet.z - z, 2) > GetLoadRadius()) {
                toRemove.push_back(i);
            }
            i++;
        }
        for(int recX = -GetLoadRadius(); recX < GetLoadRadius(); recX++) {
            for(int recZ = -GetLoadRadius(); recZ < GetLoadRadius(); recZ++) {
                bool cont = false;
                for(auto worldlet: worldlets) {
                    if (worldlet.x == recX && worldlet.z == recZ) {
                        cont = true;
                        break;
                    }
                }
                for(auto loading: loadingWorldlets) {
                    if (loading.x == recX && loading.z == recZ) {
                        cont = true;
                        break;
                    }
                }
                if(cont || pow(recX - x, 2) + pow(recZ - z, 2) > GetLoadRadius())
                    continue;

                std::ifstream file("./worlds/" + worldName + "/" + prefix + std::to_string(recX) + "X" + std::to_string(recZ) + "Z.json");
                if (!file)
                    continue;

                LoadingWorldlet loading;
                loading.x = recX;
                loading.z = recZ;
                loading.result = std::async(LoadWorldlet, recX, recZ, file);
                loadingWorldlets.push_back(loading);
            }
        }
    }
}

我尝试添加默认构造函数,但后来收到有关missing =运算符的错误。编译器不会自动添加这些东西吗?我该如何修复错误?如果它很重要,我正在使用Visual Studio 2013预览版。

1 个答案:

答案 0 :(得分:2)

查看代码,您需要一种方法来实例化对std::future<Worldlet> &result;的引用

通常,这是通过构造函数完成的。

struct LoadingWorldlet 
{
    LoadingWorldlet( std::future<Worldlet> & inWorldlet ): 
        result( inWorldlet ) {}

    int x, z;
    std::future<Worldlet> &result; 
};

否则,您可能根本就没有将数据成员作为引用(这假设其他数据成员也没有强制构造函数):

std::future<Worldlet> result;