Making a function like this part of a class

This is a discussion on Making a function like this part of a class within the C++ Programming forums, part of the General Programming Boards category; I've attempted doing this many way but no matter what i get errors. I'd like to do it with a ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    106

    Making a function like this part of a class

    I've attempted doing this many way but no matter what i get errors. I'd like to do it with a .h file and a corresponding .cpp file for functions.. Any help is appreciated.
    Thanks.
    Also I felt like this was more of a general programing question which is why I posted it on the c++ board.

    Code:
    SDL_Surface *getImage(std::string filename, int imageType)
    {
        SDL_Surface* image;
        switch(imageType)
        {
            case BMP_IMAGE:
            image = SDL_LoadBMP(filename.c_str());
            break;
    
            case JPEG_IMAGE:
            break;
        }
        if(image == NULL)
        {
            return NULL;
        }
        image = SDL_DisplayFormat(image);
        return image;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    So what were the errors?

    > I'd like to do it with a .h file and a corresponding .cpp file for functions.
    Well the approach seems good at least, so I figure it's just matter of details.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    106
    okay thanks for the reply. I believe it's where I'm putting the *.

    so I have Image.h and Image.cpp, I already have my draw functions for rendering a background image, and a sprite. Now I'm trying to use my function that I have from the main.cpp and implement it in the class. Here's the errors I'm getting:

    Code:
    C:\Users\Cody\Desktop\Anotherattempatclasses\src\Image.cpp||In function 'SDL_Surface Image::* getImage(std::string, int)':|
    C:\Users\Cody\Desktop\Anotherattempatclasses\src\Image.cpp|55|error: cannot convert 'SDL_Surface*' to 'SDL_Surface Image::*' in return|
    C:\Users\Cody\Desktop\Anotherattempatclasses\src\Image.cpp|56|warning: control reaches end of non-void function|
    ||=== Build finished: 1 errors, 1 warnings ===|
    Image.h
    Code:
    #ifndef IMAGE_H
    #define IMAGE_H
    #include <string>
    #include "sdl\sdl.h"
    
    #define BMP_IMAGE 0
    #define JPEG_IMAGE 1
    
    class Image
    {
        public:
            Image();
            virtual ~Image();
            void addSurface(int, int, SDL_Surface*, SDL_Surface*);
            void drawSprite(int, int, int, int, int, int, SDL_Surface*, SDL_Surface*);
        protected:
        private:
    };
    
    #endif // IMAGE_H
    Image.cpp
    Code:
    #include "Image.h"
    
    Image::Image()
    {
        //ctor
    }
    
    Image::~Image()
    {
        //dtor
    }
    
    void Image::addSurface(int x, int y, SDL_Surface* source, SDL_Surface* dest)
    {
        SDL_Rect box;
        box.x = x;
        box.y = y;
        SDL_BlitSurface(source, NULL, dest, &box);
    }
    
    void Image::drawSprite(int srcX, int srcY, int dstX, int dstY, int width, int height, SDL_Surface* source, SDL_Surface* dest)
    {
        SDL_Rect src;
        src.x = srcX;
        src.y = srcY;
        src.w = width;
        src.h = height;
    
        SDL_Rect dst;
        dst.x = dstX;
        dst.y = dstY;
        dst.w = width;
        dst.h = height;
    
        SDL_BlitSurface(source, &src, dest, &dst);
    }
    
    SDL_Surface Image::*getImage(std::string filename, int imageType)
    {
        SDL_Surface* image;
        switch(imageType)
        {
            case BMP_IMAGE:
            image = SDL_LoadBMP(filename.c_str());
            break;
    
            case JPEG_IMAGE:
            break;
        }
        if(image == NULL)
        {
            return NULL;
        }
        image = SDL_DisplayFormat(image);
        return image;
    }
    Thanks... And looking at the errors now they do not really make sense to me because it's about the return, but it worked fine in the main.cpp

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    106
    okay so this is why I hate/love posting things on forums.. normally I find my mistakes right after wards.. found out the mistake here was that in fact the asterik was in the wrong place. And also, after I got that problem fixed I discovered I forgot to put a function prototype..(bad me) I really need a book on object oriented design concepts or something. I'm having a little trouble wrapping my head around interaction between classes.. Any suggestions?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jamort View Post
    okay so this is why I hate/love posting things on forums.. normally I find my mistakes right after wards.. found out the mistake here was that in fact the asterik was in the wrong place. And also, after I got that problem fixed I discovered I forgot to put a function prototype..(bad me) I really need a book on object oriented design concepts or something. I'm having a little trouble wrapping my head around interaction between classes.. Any suggestions?
    Well, I just finished the "Thinking in C++" books ... they helped alot. Volume 1 spends a lot of time taking us from C-think to C++ by gradually progressing from variable to struct to class... I thought it was very well done. Mind you a couple of spots near the end of volume 1 and most of volume 2 had my head just spinning.... Lots of re-reading going on.

    Bruce Eckel's Free Electronic Books (Thinking in...)

  6. #6
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    By the way... SDL_LoadBMP is kind of pointless. Why don't you use the SDL_image extension library? IMG_Load() can handle a large number of images including bmp, png, tga etc. Also, the library has a function for determining what kind of image you loaded.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling GNU MP
    By mattnp12 in forum C Programming
    Replies: 3
    Last Post: 06-23-2011, 04:58 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21