gpt4 book ai didi

c++ - 错误 : qualifiers dropped in binding reference of type "blah blah" to initialize "some other blah blah"

转载 作者:行者123 更新时间:2023-11-28 04:06:55 34 4
gpt4 key购买 nike

所以,当我在线程外创建“boxes”和“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 Flyscene::raytraceScene(int width, int height) {
std::cout << "ray tracing ..." << std::endl;


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


// 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();
}

这里还有 rayTrace 方法:

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)

你认为这是为什么?

这是完整的错误描述:

错误(事件)E0433 限定符在“std::vector>、std::allocator>>> &”类型的绑定(bind)引用到“const std::vector>、std::allocator>>> 类型的初始化程序中删除” "光线追踪

错误(事件)E0433 限定符在类型的绑定(bind)引用中丢失“std::vector>, std::allocator>>> &”到类型“const std::vector>, std::allocator>>>”的初始化程序

最佳答案

您需要将 mutable 添加到您的 lambda 中。

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

您的代码可以简化为这个例子:

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

当您在 lambda 中创建 vector 时,它们不会被捕获,因此您可以在 traceRay 中更改它们。

所以在第一个片段中你添加mutable:

    std::async([=, &origin, &curr_pixel, &pixel_data]() mutable
{ ^^^^^^^
while (true)
{

关于c++ - 错误 : qualifiers dropped in binding reference of type "blah blah" to initialize "some other blah blah",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58573008/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com