简体   繁体   English

C++ CImg 访问冲突读取位置

[英]C++ CImg Access violation reading location

I currently try to learn how to use the CImg library but I get an error that I do not quite understand.我目前正在尝试学习如何使用 CImg 库,但出现了一个我不太明白的错误。 The documentation of the constructors in the CImg library were also rather unhelpful. CImg 库中构造函数的文档也相当无用。 The basic idea is that I want to create an image so that I can write the pixels in a second step (not shown) manually.基本思想是我想创建一个图像,以便我可以在第二步(未显示)中手动写入像素。 However, the access to the image created with the CImg constructor is denied for some reason.但是,由于某种原因,对使用 CImg 构造函数创建的图像的访问被拒绝。

Here is a minimal example:这是一个最小的例子:

#include <iostream>
#include <CImg.h>
#include "Header.h"

using namespace std;

int Main() {

    cimg_library::CImg<float> img(200, 200, 0, 3, 0.0);
    cout << "img is: " <<  img(0, 0, 0, 0) << endl;  //<----------------- error occurs here

    return 0;
}

The error reads: Exception thrown at 0x0067B693 in Test.exe: 0xC0000005: Access violation reading location 0x00000000错误内容为:Test.exe 中的 0x0067B693 处抛出异常:0xC0000005:访问冲突读取位置 0x00000000

Any help in understanding this would be much appreciated!任何帮助理解这一点将不胜感激!

Best最好的事物

Blue蓝色

# #

Edit: I tried 2 more things but both don't work unfortunately.编辑:我尝试了另外两件事,但不幸的是两者都不起作用。

1st: I tried the .data() function as well but to no avail.第一:我也尝试了 .data() 函数,但无济于事。 Changing data types (float, int, unsigned char) also did not solve the problem, apart from giving the error message that the whole thing would point to a NULL vector now (still access denied).更改数据类型(float、int、unsigned char)也没有解决问题,除了给出整个事情现在将指向空向量的错误消息(仍然拒绝访问)。

2nd: I switched to using pointers:第二:我改用指针:

 cimg_library::CImg<unsigned char>* img = new cimg_library::CImg<unsigned char>(200, 200, 0, 3, 1); cout << "img is: " << *img->data(0, 0, 0, 0) << endl;

This still gives pretty much the same error message though: Exception thrown: read access violation.尽管如此,这仍然给出了几乎相同的错误消息:抛出异常:读取访问冲突。 cimg_library::CImg::data(...) returned nullptr. cimg_library::CImg::data(...) 返回 nullptr。

Dont set 0 but 1 for the number of slices.切片数不要设置为 0,而是设置为 1。 Otherwise you get an empty image 0x0x0x0 that has no pixels, leading to a wrong memory access.否则,您会得到一个没有像素的空图像 0x0x0x0,从而导致错误的内存访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM