Access an function of an class

Could someone please explain how to properly access a function of an class my compiler gives that error:
error: cannot call member function '' without object

My book describes that part of classes a bit poorly.

Here is the current 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <SDL/SDL.h>
#include "SDL/SDL_image.h"
#include <string>
using namespace std;

const int FPS=30;
const int FENSTER_HOEHE=640;
const int FENSTER_BREITE=480;
const int FENSTER_FARBTIEFE=32;
SDL_Surface* screen,*image,*optimizedImage,*background;

class object
{
	public:
		SDL_Rect body;
		int objgroup;
		int collisiongroup;

	SDL_Rect make(int rx, int ry, int rw, int rh)
	{
		SDL_Rect body;
		body.x=rx;
		body.y=ry;
		body.w=rw;
		body.h=rh;
		return body;
	}
};

SDL_Surface *optimage(string filename);

int main(int argc, char** argv)
{
	//initialisierung der elemente
	SDL_Init(SDL_INIT_EVERYTHING);
	screen=SDL_SetVideoMode(FENSTER_HOEHE,FENSTER_BREITE,FENSTER_FARBTIEFE,SDL_SWSURFACE);
	SDL_WM_SetCaption("Test", NULL);
	image=optimage("16sprite.png");
	object obj;
	obj.body=object::make(10,10,20,20);
	Uint32 white=SDL_MapRGB(screen->format,0xff,0xff,0xff);
	Uint32 start;
	bool running=true;
	while(running)
	{
		start=SDL_GetTicks();
		SDL_Event event;
		
		while(SDL_PollEvent(&event))
		{
			switch(event.type)
				{
					case SDL_QUIT:
						running=false;
						break;
				}
		}
		
		SDL_BlitSurface(image,NULL,screen,NULL);
		SDL_FillRect(screen,&obj.body,white);
		SDL_Flip(screen);
		if(1000/FPS>SDL_GetTicks()-start)
			SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
	}
	SDL_FreeSurface(image);
	SDL_Quit();
	return 0;
}

SDL_Surface *optimage(string filename) 
{ 
	SDL_Surface* loadedImage=NULL; 
	optimizedImage=NULL; 
	loadedImage=IMG_Load(filename.c_str()); 
	
	if(loadedImage!=NULL ) 
	{ 
		optimizedImage=SDL_DisplayFormat(loadedImage); 
		SDL_FreeSurface(loadedImage); 
	} 
		return optimizedImage; 
}

The the error is in line 40 (well or in the class itself).

Thank's as always~
On line 19, the function is not static, so you have to have an instance to call it. However this looks like you were trying to make a constructor. It makes no sense otherwise for this function to be a part of the class.
is not static

So you mean i simple have to put an static in front of... oh yay it works thanks a lot. ;)
It should be an Constructor (i had it as an simple function before but if i want to make more objects, a collission system and so on i thought a class to generate objects would be better)
Even though it works, it is poor design and doesn't make sense. Just letting you know so you can eventually fix it ;)
Well.. I'm still a beginner and have to learn much (especially about writing clean/beautifull code)
A simple function would be a good solution in this situation, in my opinion. Wrapping this inside a class just adds an extra layer of complexity that is not necessary.

1
2
3
4
5
6
7
8
9
SDL_Rect* create_SDL_Rect(int x, int y, int w, int h)
{
   SDL_Rect* rect = new SDL_Rect;
   rect.x = x;
   rect.y = y;
   rect.w = w;
   rect.h = h;
   return rect;
}
Before I had it in the class it was just like that only problem if i want to add colision and non collision groups to some objects the i have to create a new int for each object/objectgroup.

Main interrest of doing it that way was also to get an better understanding of classes.

thanks~
Doing it the wrong way won't give you a better understanding ;)
Topic archived. No new replies allowed.