Passing objects to methods and acting on them.

Started by
1 comment, last by GotenRulezU 19 years, 3 months ago
What wont work is what is in my CWarrior class the method special. Any ideas why not? I tried using a pointer but it kept saying must have struct/unioin/type before .setName("BLAH"); Here is my source:

#include<windows.h>
bool gameover = false;
class CFighter{
private:
	int health;
	int maxhealth;
	int power;
	int defense;
	int speed;
	char* name;
public:
	CFighter(){
		health = 0;
		maxhealth = 0;
		power = 0;
		defense = 0;
		name = "";
	}
	int getHealth(){return health;}
	int getMaxHealth(){return maxhealth;}
	int getPower(){return power;}
	int getDefense(){return defense;}
	int getSpeed(){return speed;}
	char* getName(){return name;}
	void setHealth(int newhealth){health = newhealth;}
	void setMaxHealth(int newmaxhealth){maxhealth = newmaxhealth;}
	void setPower(int newpower){power = newpower;}
	void setDefense(int newdefense){defense = newdefense;}
	void setSpeed(int newspeed){speed = newspeed;}
	void setName(char* newname){name = newname;}
};
class CWarrior: public CFighter{
public:
	void special(CFighter fighter){
		fighter.setName("you suck");
	}
};
CWarrior Cory;
CWarrior Alex;
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hpInstance,PSTR cmdLine,int iCmd){
	WNDCLASS wc;
	HWND hwnd;
	MSG msg;
	Cory.setName("Cory Fisher");
	Cory.setHealth(100);
	Alex.setName("Alex Fisher");
	Alex.setHealth(100);
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = "window";
	wc.lpszMenuName = NULL;
	wc.style = CS_VREDRAW|CS_HREDRAW;
	RegisterClass(&wc);
	hwnd = CreateWindow("window","Fight Game Ver 0.01",WS_OVERLAPPEDWINDOW,0,0,500,500,NULL,NULL,hInstance,NULL);
	ShowWindow(hwnd,iCmd);
	UpdateWindow(hwnd);
	while(!gameover){
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else{

		}
	}
	UnregisterClass("window",hInstance);
	return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam){
	HDC hdc;
	switch(iMsg){
	case WM_DESTROY:
		PostQuitMessage(0);
		gameover = true;
		break;
	case WM_CHAR:
		switch((char)wParam){
		case VK_ESCAPE:
			PostQuitMessage(0);
			gameover = true;
			break;
		case VK_RETURN:
			hdc = GetDC(hwnd);
			TextOut(hdc,250,250,Alex.getName(),25);
			break;
		case 'i':
			hdc = GetDC(hwnd);
			TextOut(hdc,250,250,Cory.getName(),25);
			break;
		case VK_SPACE:
			Cory.special(Alex);
			break;
		}
		break;
	}
	return DefWindowProc(hwnd,iMsg,wParam,lParam);
}

Thanks,
-Goten
Advertisement
There are a few incorrect things. First, setName() is definitely wrong since you're using char* and not std::string. You have two choices, change char* to std::string or use strncpy(). Second, there is another problem in that very same function. You need to declare fighter as CFighter& or CFighter* because otherwise you will only have copy of the object. If you use the first one you will have nothing to change in your code but the declaration/definition of your function but if you chose the latter one, you will have to change fighter.setName() to fighter->setName() (you should already know this if you're into GDI & game programming...)

*EDIT* You might want to declare CFighter as an abstract class if you plan to make it a base class for multiple other classes. It will allow you to use polymorphism and make things easier...
Ex:
class CFighter {    public:        CFighter() = 0;        void Render(HDC) = 0;};class CWarrior : public CFighter {    public:        CWarrior() {}        void Render(HDC);};class CWizard : public CFighter {    public:        CWizard() {}        void Render(HDC);};// somewherestd::list<CFighter*> lsFighters;lsFighters.push_back(CWarrior());lsFIghters.push_back(CWizard());//....typedef std::list<CFighter*>::const_iterator LSCI;// rendering functionvoid Render(std::list<CFighter*>& ls, HDC hdc) {    MemDC = CreateCompatibleDC(hdc);    for(LSCI i = ls.begin(); i != ls.end(); ++i)        (*i)->Render(MemDC);    BitBlt(hdc, 0, 0, ScreenWidth, ScreenHeight, MemDC, 0, 0, SRCCOPY);    DeleteDC(MemDC);}
I didn't compile that code but you get the idea..
Knew this at one point and time but:
fighter->setName()
is what i wanted. Thanks.
-Goten

This topic is closed to new replies.

Advertisement