What is wrong with My Constructor

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>

using namespace std;

const int DefaultWidth  = 32;
const int DefaultHeight = 24;
const int 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?
Shouldn't it be pixels[0]->red;
I figured it out.

On line 33, it should be

pixels = new RGB[depth*height];

Topic archived. No new replies allowed.