Initialize an object?

Hi…,

I am new to C++. I would like to initialize my “board” object in testApp.cpp, but how?

e.g.

  
  
board = new board2D(10,12);  
  

doesn’t work.

The testApp.h code follows here:

  
  
#pragma once  
  
#include "ofMain.h"  
  
class cell {  
	public:  
	cell(){  
	}  
	int dir;  
};  
  
class board2D{  
	public:  
	board2D(){}  
	board2D(int c, int r){  
		columns = c;  
		rows = r;  
		cellPtr = new cell[c*r];  
	}  
  
	~board2D(){  
		delete[] cellPtr;  
	}  
  
	private:  
		int columns;  
		int rows;  
		cell *cellPtr;  
};  
  
class testApp : public ofBaseApp{  
  
	public:  
		void setup();  
		void update();  
		void draw();  
  
		void keyPressed  (int key);  
		void keyReleased(int key);  
		void mouseMoved(int x, int y );  
		void mouseDragged(int x, int y, int button);  
		void mousePressed(int x, int y, int button);  
		void mouseReleased(int x, int y, int button);  
		void windowResized(int w, int h);  
		void dragEvent(ofDragInfo dragInfo);  
		void gotMessage(ofMessage msg);  
		  
		int debug;  
		board2D board;  
};  
  

Thanks,

Mike

I think you need to declare your board var in testApp.h a pointer, e.g. board2D *board; Otherwise it is statically initialized and so using new() on it does not work.

You could also declare it as board2D board(10, 12); in the header but most people dont really like that sort of thing.

Another option is to skip the constructor and just have a setup/init method. What I usually do is write a setup method then write a constructor which just calls the setup method,eg

  
  
someclass::setup(int width, int height, int cnt)  
{  
...  
}  
  
someclass::someclass(int count) { setup(100, 100, count); }  
someclass::someclass(int w, int h) { setup(w, h, 0); }  
  

Hi pants,

thank you for your help. I have used now a init method to fill in my object.

Best regards

Mike