I was running into run time errors, and stripped down my code to isolate the problem. From that I was able to write this little piece of code to demonstrate my issue:

#include <windows.h>

class test
{
private:
	int a;
public:
	test();
	void run();
};

test::test()
{
	a = 5;
}
void test::run()
{
	a = 6; // comment this to prevent the runtime error.

}



int WINAPI WinMain(	HINSTANCE	hInstance,			// Instance
					HINSTANCE	hPrevInstance,		// Previous Instance
					LPSTR		lpCmdLine,			// Command Line Parameters
					int			nCmdShow)			// Window Show State
{
	test* t;
	t->run();

}

This gives me an unhandled exception in visual studio c++ 2005. I don't understand what's bad about it. Making anything static is not an option and neither is a declaring the "a" variable in the cpp file (in my version, all of this is split up properly between header and cpp).

Any insight on how to better handle this would be greatly appreciated!

Try using this:

test obj;
	test *t = &obj;
	t->run();

test* t;
t->run();

Try using this:

test obj;
	test *t = &obj;
	t->run();

instead of:

test* t;
	t->run();

Cheers

commented: very helpful +0

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.