C ++ CImg访问冲突读取位置 [英] C++ CImg Access violation reading location

查看:181
本文介绍了C ++ CImg访问冲突读取位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试学习如何使用CImg库,但是遇到了一个我不太了解的错误. CImg库中的构造函数文档也没有帮助.基本思想是,我想创建一个图像,以便可以在第二步中手动写入像素(未显示).但是,由于某些原因,拒绝使用CImg构造函数创建的图像的访问.

I currently try to learn how to use the CImg library but I get an error that I do not quite understand. The documentation of the constructors in the CImg library were also rather unhelpful. 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.

这是一个最小的示例:

#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;
}

该错误显示为: 在Test.exe中的0x0067B693处引发异常:0xC0000005:访问冲突读取位置0x00000000

The error reads: Exception thrown at 0x0067B693 in Test.exe: 0xC0000005: Access violation reading location 0x00000000

我们将不胜感激!

最佳

蓝色

我又尝试了2件事,但不幸的是,这两项都无法正常工作.

I tried 2 more things but both don't work unfortunately.

1st:我也尝试了.data()函数,但无济于事.更改数据类型(float,int,unsigned char)也无法解决问题,除了给出错误消息,即整个事件现在都指向NULL向量(仍然拒绝访问).

1st: I tried the .data() function as well but to no avail. 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).

2nd:我改用了指针:

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;

尽管如此,它仍然给出几乎相同的错误消息: 引发异常:读取访问冲突. cimg_library :: CImg :: data(...)返回了nullptr.

This still gives pretty much the same error message though: Exception thrown: read access violation. cimg_library::CImg::data(...) returned nullptr.

推荐答案

不要将切片数设置为0,而将其设置为1.否则,您将得到一个没有像素的空图像0x0x0x0,从而导致错误的内存访问.

Dont set 0 but 1 for the number of slices. Otherwise you get an empty image 0x0x0x0 that has no pixels, leading to a wrong memory access.

这篇关于C ++ CImg访问冲突读取位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆