3

当我尝试编译此文件时,我收到错误 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);
            }
        }
    }
}

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

4

1 回答 1

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;

于 2013-08-15T22:20:01.380 回答