C++ 错误:类型为“的绑定引用中删除了限定符”;诸如此类;初始化";其他一些胡说八道;

C++ 错误:类型为“的绑定引用中删除了限定符”;诸如此类;初始化";其他一些胡说八道;,c++,graphics,eigen,C++,Graphics,Eigen,因此,当我在线程外创建“box”和“boxbound”变量时,我得到了一个运行时错误,但是当我在线程内的for循环内移动它时,错误消失了,原因是什么 void Flyscene::raytraceScene(int width, int height) { std::cout << "ray tracing ..." << std::endl; //start of acceleration structure std::vector<std::vector<

因此,当我在线程外创建“box”和“boxbound”变量时,我得到了一个运行时错误,但是当我在线程内的for循环内移动它时,错误消失了,原因是什么

void Flyscene::raytraceScene(int width, int height) {
std::cout << "ray tracing ..." << std::endl;

//start of acceleration structure
std::vector<std::vector<Tucano::Face>> boxes = firstBox(mesh);
std::vector<std::vector<Eigen::Vector3f>> boxbounds;
for (int i = 0; i < boxes.size(); i++) {
    boxbounds.push_back(getBoxLimits(boxes[i], mesh));
}
/////


// if no width or height passed, use dimensions of current viewport
Eigen::Vector2i image_size(width, height);
if (width == 0 || height == 0) {
    image_size = flycamera.getViewportSize();
}


// create 2d vector to hold pixel colors and resize to match image size
vector<vector<Eigen::Vector3f>> pixel_data;
pixel_data.resize(image_size[1]);
for (int i = 0; i < image_size[1]; ++i)
    pixel_data[i].resize(image_size[0]);


// origin of the ray is always the camera center
Eigen::Vector3f origin = flycamera.getCenter();
Eigen::Vector3f screen_coords;



// Multi Threading
// Comment this if you don't want multi-threading
//-----------------------------------------------------//
int max_pixels = (image_size[0] * image_size[1]); //width * height
// Get amount of cores of your CPU
int cores = std::thread::hardware_concurrency();
// Keep track of # of pixels (atomic making sure no 2 threads render the same pixel)
volatile std::atomic<std::size_t> curr_pixel(0);
// Stores all cores assigned to a task
std::vector<std::future<void>> future_vector;
cout << "Threads supported: " << cores << "\n";
while (cores--)
    future_vector.emplace_back(
        std::async([=, &origin, &curr_pixel, &pixel_data]()
            {
                while (true)
                {
                    int index = curr_pixel++;
                    if (index >= max_pixels)
                        break;
                    std::size_t i = index % image_size[1];
                    std::size_t j = index / image_size[1];
                    //cout << "at index: " << index << std::endl;


                    // create a ray from the camera passing through the pixel (i,j)
                    auto screen_coords = flycamera.screenToWorld(Eigen::Vector2f(i, j));
                    // launch raytracing for the given ray and write result to pixel data
                    pixel_data[i][j] = traceRay(0,origin, screen_coords, boxes, boxbounds);
                    if (index % 10000 == 0) {
                        std::cout << "Percentage done (mt): " << (float)(index / 10000) << "%" << std::endl;
                    }
                }
            }));

// Call futures (Async jobs), this will activate all process on the cores
for (auto& e : future_vector) {
    e.get();
}
void flysecene::raytraceScene(int-width,int-height){

std::cout您需要向lambda添加
mutable

向量通过引用传递(到
traceRay
),因此可以在此函数内修改它们。您的lambda通过复制获取向量
=
(用于捕获),只能读取
=
捕获的对象,您无法修改它们

您的代码可以简化为以下示例:

void bar(std::vector<int>& v) {

}

void foo() {
    std::vector<int> v;

    auto l = [=]() /*mutable*/
    {
        bar(v); // works only with uncommented mutable
        // v can be modified only with mutable
    };
    l();
}

键入“…运行时错误…”->编译时错误?放“废话”在你的问题中,标题对你认真回顾你的问题没有任何好处。只是说。嘿,首先,是的编译时错误,我的不好。其次,我胡说八道,否则它不适合标题。我也收到了一些严肃的回答。但是谢谢,我会在我以后的帖子中记住它。记住它现在,在这篇文章中,使用edit函数。当你编辑它的时候,试着做一个修改,也就是说,删除所有不相关的部分,但提供实际可以编译的代码(不要忽略include、类声明等)
Eigen::Vector3f Flyscene::traceRay(int level, Eigen::Vector3f& origin, Eigen::Vector3f& dest, std::vector<std::vector<Tucano::Face>>& boxes, std::vector<std::vector<Eigen::Vector3f>>& boxbounds)
void bar(std::vector<int>& v) {

}

void foo() {
    std::vector<int> v;

    auto l = [=]() /*mutable*/
    {
        bar(v); // works only with uncommented mutable
        // v can be modified only with mutable
    };
    l();
}
    std::async([=, &origin, &curr_pixel, &pixel_data]() mutable
        {                                               ^^^^^^^
            while (true)
            {