I'm doing a project for class, and I have to work with Dynamic arrays. I am trying to setup a constructor for the image class that sets each pixel to black. *pixels is an array of Struct RGB. Here is some sample code.
#include <iostream>
usingnamespace std;
constint DefaultWidth = 32;
constint DefaultHeight = 24;
constint DefaultDepth = 255;
struct RGB
{
int red,
green,
blue;
};
const RGB Black = {0, 0, 0};
class Image
{
public:
RGB *pixels;
int width,
height;
int depth;
Image()
{
width = DefaultWidth;
height = DefaultHeight;
depth = DefaultDepth;
RGB *pixels = new RGB[depth*height];
for ( int i = 0; i < (depth*height) ; i ++)
pixels[i] = Black;
}
};
int main(){
Image testObject;
cout << testObject.pixels[0].red;
}
When I run it, I just simply declare an Image object for testing. I am getting an unhandled exception. I think my constructor is not setting each pixel to the constant black. How can I fix this constructor?