"error: expected constructor, destructor, or type conversion before ...

Hello all, I'm messing around with SDL in C++ and I've encountered this error while trying to create an array of a class type "Entity".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Entity {
	public:
		SDL_Surface image;
		SDL_Rect loc;
		bool operator!= (Entity otherent);
		bool operator== (Entity otherent);
		bool anim;
		Entity(SDL_Surface img, SDL_Rect bbox, bool isanim) {
			image = img;
			loc = bbox;
			anim = isanim;
		}
		Entity() {
		}
		void live(){
			//Nothing here yet
		}
};


Thats the class. Following some examples I've seen before, I tried to do the following.

1
2
3
Entity entitylist[64];
SDL_Surface fourbyoneblock = load_image("4x1block.bmp");
entitylist[0] = new Entity(fourbyoneblock, fourbyoneblock.clip_rect, false);


Which results in "error: expected constructor, destructor, or type conversion before ‘=’ token" at entitylist[0] = etc etc.

I've been trying to get to the cause but I guess its just something I'm missing, I don't know C++ particularly well anyway, anyone else have any ideas?
entitylist is defined as an array of 64 objects.

new creates a new object dynamically and returns a pointer to it.

You are trying to assign that pointer to an existing object, which you can't do.

Instead, you can either:

1) don't use a ctor to initialize those members... and instead have an "init" function or something similar:

1
2
Entity entitylist[64];
entitylist[0].Init(fourbyoneblock, ... );


2) change your entitylist to be an array of pointers instead of an array of objects:

1
2
3
4
5
Entity* entitylist[64];
entitylist[0] = new Entity( ... );

// remember to delete anything you new:
delete entitylist[0]
Hey Disch!

Thanks for that, I didn't know that and it would have caused issues down the road, but it doesn't appear to be the cause of the error, as it still appears with both of those variations.
I believe all your SDL_Surface should be pointers
SDL_Surface *image
even in
Entity(SDL_Surface *img, SDL_Rect bbox, bool isanim)

SDL_Surface *fourbyoneblock = load_image("4x1block.bmp");
Last edited on
Hey DrakeMagi!

Also true, they were originally all pointers, but I couldn't access the attributes of the surfaces like "*image.w", probably more just my lack of C++ knowledge rather than it not being possible, so for a while a kept then non-pointer and just "&image" when ever I used a function that required a pointer, and am planning to get to the bottom of how to do it properly, but it wasn't needed at the time.

And I don't think that'd fix the error either.

Last edited on
It won't, but by not making them pointers, it becomes really expensive to copy
an Entity object, and your operator== and operator!= both copy their right-hand
side onto the stack.
but I couldn't access the attributes of the surfaces like "*image.w",


You'd use the -> operator:

 
image->w
@JSmith, I know why I should make them pointers, but it was just an inconvenience at the time caused by me not knowing enough about C++, but there was a bigger error that caused the code to not work at all, so I've focussed on that.

@Disch Thanks! I took the time to change it all to pointers and the references to "image->w".

Still no idea whats causing the error though?
Last edited on
post error !
What are you compile with ?
What is your editor ?

if you can use vector:
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
#include "SDL/SDL.h"
#include <vector>

class Entity
{
	public:
		SDL_Surface *image;
		SDL_Rect loc;
		bool operator!= (Entity otherent);
		bool operator== (Entity otherent);
		bool anim;
	
		Entity(SDL_Surface *img, SDL_Rect bbox, bool isanim) 
		{
			image = img;
			loc = bbox;
			anim = isanim;
		}
		Entity(){}
		
		void live(){}
};
	
int main( int argc , char* args[] )
{
	std::vector<Entity> test;
	SDL_Surface *one = load_image("Your.bmp");
	SDL_Rect rect = {10,10};
	Entity tmp(one , rect , false);
	test.push_back( tmp );
	
	return 0;
}
Hey it was originally "error: expected constructor, destructor, or type conversion before ‘=’ token" at entitylist[0] = etc etc"

I decided to try using vectors, now it complains about "expected constructor destructor or type conversion before "." at entitylist.pushback( tmp ); So whenever I try to use entitylist, this error pops up.

Oh, G++ 4.3 is my compiler.
The code i gave you compiles for me with out any errors.

this code runs/compile for me. I would need to see code and all errors ?
this code has some minor changes .
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
#include "SDL/SDL.h"
#include <vector>

class Entity
{
	public:
		SDL_Surface *image;
		SDL_Rect loc;
		bool operator!= (Entity otherent);
		bool operator== (Entity otherent);
		bool anim;
	
		Entity()
		{
			SDL_Rect locx = { 0 , 0 , 0 , 0 };
			image = NULL;
			loc = locx;
			anim = false;
		}
		
		~Entity()
		{
		}
		
		void E_Set(SDL_Surface *img, SDL_Rect bbox, bool isanim) 
		{
			image = img;
			loc = bbox;
			anim = isanim;
		}
		
		void live(){}
};
	
int main( int argc , char* args[] )
{	
	SDL_Init( SDL_INIT_EVERYTHING );
	SDL_Surface *screen = NULL; 
	screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );

	std::vector<Entity> test;
	SDL_Surface *one = NULL; 
	one = SDL_LoadBMP( "grass.bmp" );
	SDL_Rect rect = {10,10};
	
	Entity tmp;
	tmp.E_Set( one , rect , false );
	test.push_back( tmp );
	
	SDL_BlitSurface( one , NULL , screen , &rect );
	rect.x = 50;
	SDL_BlitSurface( test[0].image , NULL , screen , &rect );
	
	SDL_Flip( screen );
	
	SDL_Delay( 3000 );
	
	SDL_FreeSurface ( screen );
	SDL_FreeSurface ( one );
	//SDL_FreeSurface ( test[0].image );  <-- link to image one
	
	SDL_Quit();
	
	return 0;
}
Topic archived. No new replies allowed.