# I want to put an SDL_Surface into a struct

## Recommended Posts

Here's my struct. Theres more in it but i'm only posting relevant code. This is in classes.h
struct thing
{
thing()
{};

thing(SDL_Surface image)
{
this->image = image;
}

SDL_Surface image;
}

Here's my object I want to create. This is in things.h.
thing tree1(tree);

The sdl_surface doesn't need to point to an image yet, I just want it to be named and in memory so I can use it for something else. When I try and compile this I get this error.
error C2065: 'tree' : undeclared identifier

Now obviously i'm doing something wrong but I don't know what. I have sdl and sdl_image included in both files and I have classes.h included in things.h. Is what i'm trying to do even possible?

##### Share on other sites
SDL_Surface should only ever be handled as a pointer, since it's designed to be reference counted. Copying it by value can cause some bad things to happen, so don't do that.

struct thing{    thing()    {        this->image = NULL;    };    // Note the use of pointers.  Also be sure to    // call SDL_FreeSurface(image) when you're done.    thing(SDL_Surface *image)    {        this->image = image;    }    SDL_Surface *image;}

However, that's not what's causing the compiler error here. Your problem is that "thing tree1(tree);" is interpreting 'tree' as a type name, and you have not defined this before referring to it.

##### Share on other sites
Quote:
 However, that's not what's causing the compiler error here. Your problem is that "thing tree1(tree);" is interpreting 'tree' as a type name, and you have not defined this before referring to it.

Why is it doing that? Isn't SDL_Surface the type name? What am I supposed to be defining?

##### Share on other sites
Hi,

How have you declared : tree ?
Chances are that you didn't declare it correctly, besides you should take the advice of hh10k about using pointer.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628312
• Total Posts
2981998

• 9
• 9
• 13
• 11
• 13