Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 获取和设置三维数组_C++_Arrays_Image_Multidimensional Array - Fatal编程技术网

C++ 获取和设置三维数组

C++ 获取和设置三维数组,c++,arrays,image,multidimensional-array,C++,Arrays,Image,Multidimensional Array,我必须从一个类中获取一个三维数组c[]w[]h[],并将其转换为一个无符号字符[]的一维数组。我试过这样做。但是它不起作用!!。。当我通过命令行输入时,执行会停止并中断 实施: #include <iostream> #include<fstream> #include<stdlib.h> #include<vector> //#include "E:\Marvin_To_UnsignedChar\MetisImg.hpp" //#include

我必须从一个类中获取一个三维数组c[]w[]h[],并将其转换为一个无符号字符[]的一维数组。我试过这样做。但是它不起作用!!。。当我通过命令行输入时,执行会停止并中断

实施:

#include <iostream>
#include<fstream>
#include<stdlib.h>
#include<vector>
//#include "E:\Marvin_To_UnsignedChar\MetisImg.hpp"
//#include "C:\Users\padmanab\Documents\Visual Studio 2013\Projects\Marvin_To_UnsignedChar\MetisImg.hpp"
extern "C"
{
#include "C:\Users\padmanab\Desktop\Marvin_To_UnsignedChar\multiplyImage\multiplyImage.h"
//#include "C:\Users\padmanab\Documents\Visual Studio 2013\Projects\Marvin_To_UnsignedChar\multiplyImage\multiplyImage.h"
}

using namespace std;

class Marvin_To_UnsignedChar
{
public:
    int Color; 
    int Width; 
    int Height;
    std::vector<unsigned char> values;
    Marvin_To_UnsignedChar(int c, int w, int h) : Color(c), Width(w), Height(h), values(c*w*h){}
    unsigned char operator()(int color, int width, int height) const
    {
        return values[Height*Width*color + Height*width + height];
    }
    unsigned char& operator()(int color, int width, int height) 
    {
        return values[Height*Width*color + Height*width + height];
    }
};
#包括
#包括
#包括
#包括
//#包括“E:\Marvin\u To\u UnsignedChar\MetisImg.hpp”
//#包括“C:\Users\padmanab\Documents\Visual Studio 2013\Projects\Marvin\u To\u UnsignedChar\MetisImg.hpp”
外部“C”
{
#包括“C:\Users\padmanab\Desktop\Marvin\u To\u UnsignedChar\multiplyImage\multiplyImage.h”
//#包括“C:\Users\padmanab\Documents\Visual Studio 2013\Projects\Marvin\u To\u UnsignedChar\multiplyImage\multiplyImage.h”
}
使用名称空间std;
类Marvin_到_UnsignedChar
{
公众:
内色;
整数宽度;
内部高度;
std::向量值;
Marvin_To_UnsignedChar(intc,intw,inth):颜色(c),宽度(w),高度(h),值(c*w*h){
无符号字符运算符()(整数颜色、整数宽度、整数高度)常量
{
返回值[高度*宽度*颜色+高度*宽度+高度];
}
无符号字符和运算符()(整数颜色、整数宽度、整数高度)
{
返回值[高度*宽度*颜色+高度*宽度+高度];
}
};
在Main()中:

int颜色;整数宽度;内部高度;
标准:颜色>颜色;
std::cout>宽度;
标准:cout>高度;
马文字符到无符号字符M到V(颜色、宽度、高度);
无符号字符测试=M_到V(颜色、宽度、高度);

std::cout类代码很好。问题在于

unsigned char test = M_To_V(color, width, height); 
您正在调用
操作符()
,将尺寸作为参数(请记住,您在
颜色
宽度
高度
之前使用了
Marvin\u to\u UnsignedChar
对象),因此它将有效地输出
值[color*width*height]
,这是向量末尾的一个元素。否则代码就可以了,您可能需要使用

unsigned char test = M_To_V(x, y, z);

其中
x,y,z
是这样的
x“当我通过命令行给出输入时,执行会停止并中断…”你这是什么意思?您遇到了什么错误?
问题签名:问题事件名称:APPCRASH应用程序名称:Marvin_To_UnsignedChar.exe应用程序版本:0.0.0.0应用程序时间戳:53848d56故障模块名称:Marvin_To_UnsignedChar.exe故障模块版本:0.0.0故障模块时间戳:53848d56异常代码:c0000005异常偏移量:0000139a操作系统版本:6.1.7601.2.1.0.256.48区域设置ID:1033附加信息1:0a9e附加信息2:0a9e372d3b4ad19135b953a78882e789附加信息3:0a9e附加信息4:0a9e372d3b4ad19135b953a78882e789
Hi,但问题是当我给出像
Color=3,width=777,height=600这样的维度时,它就太过分了这是图像的实际尺寸(以像素为单位)。但这也给这些参数带来了同样的问题。我很困惑我到底做错了什么@我不确定我明白了。。。您可以很好地创建数组,
Marvin_To_UnsignedChar M_To_V(37777600),然后设置/获取您想要的任何元素,只要您没有越界。在
C/C++
中,要访问数组,可以将其定义为
intx[3][4][5]
,每个索引都应该严格小于维度,即
x[2][3][4]
是最后一个元素,而不是
x[3][4][5]
(因为索引的0-start-convention)。请查看修改后的代码,它工作正常,请告诉我您从何处获得错误。将
operator()
中的
[]
替换为
at()
,以便在越界时通知您。在上面的代码中这样做。
unsigned char test = M_To_V(x, y, z);
#include <iostream>
#include <vector>

using namespace std;

class Marvin_To_UnsignedChar
{
public:
    int Color; 
    int Width; 
    int Height;
    std::vector<unsigned char> values;
    Marvin_To_UnsignedChar(int c, int w, int h) :
            Color(c), Width(w), Height(h), values(c*w*h,'x'){}
    unsigned char operator()(int color, int width, int height) const
    {
        return values.at(Height*Width*color + Height*width + height);
    }
    unsigned char& operator()(int color, int width, int height) 
    {
        return values.at(Height*Width*color + Height*width + height);
    }
};

int main()
{
    int color = 3, width = 777, height = 600;

    Marvin_To_UnsignedChar M_To_V(color,width,height);
    M_To_V(2, 776, 599)='a'; // set the last element to `a`
    std::cout << M_To_V(2, 776, 599) << '\n';
    unsigned char test = M_To_V(1, 200, 300); // this element is pre-initialized with 'x'
    std::cout << test << '\n';
}