Skip to main content
gamedev.net
🔒 Locked

Function to make new class instance and return... failed.

Started by JS Lemming 2 replies 700+ views
Original Post
JS Lemming
JS Lemming
Hello peoples! I've run into a brick wall. See, I wanted to make a function that created an instance of a class... and then returned it so I could use it later. This is what I got:

class Image {
public:
   pvr_ptr_t img;
   int w;
   int h;
};


Image* LoadImage(const char* file, int width, int height)
{
   //Create instance
   Image* ImageName = new Image;
   ImageName.img = pvr_mem_malloc(width*height*2);
   ImageName.w = width;
   ImageName.h = height;
   png_to_texture(file, ImageName.img, PNG_FULL_ALPHA);

   return ImageName;
}
And I called it like so:

Image* p_Room = LoadImage("/rd/room.png",256,256); 
But this is what VC++ 6.0 shot at me.

In file included from ChainSawR5.cpp:20:
Image.cpp: In function `Image* LoadImage(const char*, int, int)':
Image.cpp:14: request for member `img' in `ImageName', which is of
non-aggregate type `Image*'
Image.cpp:15: request for member `w' in `ImageName', which is of non-aggregate
type `Image*'
Image.cpp:16: request for member `h' in `ImageName', which is of non-aggregate
type `Image*'
Image.cpp:17: request for member `img' in `ImageName', which is of
non-aggregate type `Image*'
NMAKE : fatal error U1077: 'C:\DevKitDC\bin\g++.exe' : return code '0x1'
Stop.
Error executing NMAKE.

ChainSawR5.exe - 1 error(s), 1 warning(s)
Did I make it clear what I'm tring to do? Any help on this is greatly appreciated.
Down with the cotton gin!
Apocryphiliac
Apocryphiliac
When accessing class members through a pointer in C++, you use the -> operator instead of "." So it would be "ImageName->img" instead of "ImageName.img"
MaulingMonkey
MaulingMonkey
Quote:
Original post by Apocryphiliac
When accessing class members through a pointer in C++, you use the -> operator instead of "." So it would be "ImageName->img" instead of "ImageName.img"


For completeness: "(*ImageName).img" would also work.

OP: Also, that variable is... oddly named. Seems to be more like an image rather than an image's name, dosn't it?
JS Lemming
JS Lemming
Thanks! Yeah, I'm kinda crusty on my C++.
Down with the cotton gin!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.