Results 1 to 2 of 2

Thread: [RESOLVED] Add an object of class to an array of objects

  1. #1

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Resolved [RESOLVED] Add an object of class to an array of objects

    what am I doing wrong here? It is throwing runtime error on highlighted line
    Code:
    //My class
    struct MYPOINT
    {
    	int X;
    	int Y;
    };
    struct MYPALETTE
    {
    	char R;
    	char G;
    	char B;
    };
    class MYPIXEL
    {
    private:
    	MYPOINT currentPoint;
    	MYPALETTE currentPalette;
    public:
    	MYPOINT getCurrentPoint() { return currentPoint; };
    	MYPALETTE getCurrentPalette() { return currentPalette; };
    	void setCurrentPoint(MYPOINT point) { currentPoint = point; };
    	void setCurrentPalette(MYPALETTE pal) { currentPalette = pal; };
    };
    
    class MyImageClass
    {
    	int Width;
    	int Height;
    	char ImagePath[_MAX_PATH];
    	MYPIXEL* pixels;
    	char* ImgData;
    
    public:
    	MyImageClass() { };
    	~MyImageClass() { if(pixels) delete [] pixels; };
    	//void InitializePixelArray() { for(int count = 0; count < Width * Height; count++) pixels[count] = new MYPIXEL(); };
    	void setWidth(int w) { Width = w; };
    	void setHeight(int h) { Height = h; };
    	void setImagePath(char* path) { strcpy(ImagePath, path); };
    	void addPixel(MYPIXEL pxl, int location) { pixels[location] = pxl; };
    	int getWidth() { return Width; };
    	int getHeight() { return Height; }; 
    	MYPIXEL* getImagePixels() { return pixels; };
    	char* getImageData() { return ImgData; }; 
    	char* getImagePath() { return ImagePath; };
    	void ReadImagePixels();
    	void ReadImageData(MYPIXEL*);
    }
    
    //ReadImagePixels
    void MyImageClass::ReadImagePixels()
    {
    	FILE* _IN_FILE;
    	_IN_FILE = fopen(ImagePath, "rb");
    
    	int pixelCount = 0;
    
    	for(int x = 0; x < Width; x++)
    	{
    		for(int y = 0; y < Height; y++)
    		{
    			char c = fgetc(_IN_FILE);
    			MYPIXEL pxl;
    			struct MYPOINT point;
    			struct MYPALETTE pal;
    
    			point.X = x;
    			point.Y = y;
    
    			pal.R = 100;
    			pal.G = 100;
    			pal.B = 100;
    
    			pxl.setCurrentPoint(point);
    			pxl.setCurrentPalette(pal);
    
    			addPixel(pxl, pixelCount);
    
    			pixelCount++;
    		}
    	}
    }
    Basically, whenever I try to add the new MYPIXEL object to array pixels (in my image class), it gives me a runtime error and closes the program. Sorry, been long time I worked in C++, and never worked in VS 2008.

    Please this is urgent.

    Thank you.

    BTW, if I remove this line or assignment, then this runs and gives me the output screen.
    Show Appreciation. Rate Posts.

  2. #2

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Add an object of class to an array of objects

    No worries! Got it.

    The pixels array was not initialized!!!
    Show Appreciation. Rate Posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width