Skip to main content
GameDev.net gamedev.net
🔒 Locked

Annoying conversion assignment problem

Started by woody1987 Oct 14, 2008 at 1:32 PM 8 replies 2k views
Original Post
woody1987
woody1987
Hi, im trying to blit two images together using SDL but everytime i compile i get:
error: cannot convert 'SDL_Surface' to 'SDL_Surface*' in assignment


I call two functions in game.cpp, marked below. The functions are in images.cpp. game.cpp

#include <string>
#include <stdlib.h>
#include <iostream>
#include "SDL/SDL.h"

#include "game.h"
#include "images.h"

using namespace std;

//attributes of the screen
const int SCREEN_BPP = 32;
//surfaces to be used
SDL_Surface *cross = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;

Game::Game()
{
	initiate();
	
	Image *image = new Image();
	//create background
	setBackground(*image);
}

Game::~Game(void)
{

}

bool Game::initiate()
{
	//Initialize all SDL subsystems 
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1) 
	{ 
		return false; 
	}
	//set up the screen
	screen = SDL_SetVideoMode(600, 400, SCREEN_BPP, SDL_SWSURFACE);
	//If there was an error in setting up the screen 
	if( screen == NULL )
	{ 
		return false; 
	}
	
	//Set the window caption 
	SDL_WM_SetCaption("Residual Hope", NULL); 
	return true;
}

int Game::setBackground(Image image)
{
    background = image.load_image("Pics/background.jpg"); <<<ERROR IS HERE
    cross = image.load_image("Pics/cross.jpg");           <<<AND HERE
    image.apply_surface(100, 100, cross, background);
    //If there was a problem in loading the image
    if(background == NULL)
    {
    	cout << "Error loading image";
        return 1;    
    }
}



images.cpp
#include <string.h>
#include <iostream>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"

#include "images.h"

using namespace std;

SDL_Surface *load_image(string filename)
{
	//image to be loaded
	SDL_Surface* loadedImage  = NULL;
	//optimized image
	SDL_Surface* optimizedImage = NULL;
	//load the image
	loadedImage = IMG_Load(filename.c_str());
	//if the image loaded
	if(loadedImage != NULL)
	{
		//create optimized image
		optimizedImage = SDL_DisplayFormat(loadedImage);
		//free the old image
		SDL_FreeSurface(loadedImage);
	}
	//return the optimized image
	return optimizedImage;
}

void Image::apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
	//temp rectangle to hold the offsets
	SDL_Rect offset;
	//give offsets to the rectangle
	offset.x = x;
	offset.y = y;
	//blit the surface
	SDL_BlitSurface(source, NULL, destination, &offset);
}




Thanks in advance
Telastyn
Telastyn
Umm, right. Cannot convert object to a pointer-to-object. Common C++ error which should be very clear to even beginners of the language. You might want to stop and make sure you have sufficient grounding so you don't get hung up on things like this in your progress...

But simply:
background = &image.load_image("Pics/background.jpg");
woody1987
woody1987
Quote:
Umm, right. Cannot convert object to a pointer-to-object. Common C++ error which should be very clear to even beginners of the language. You might want to stop and make sure you have sufficient grounding so you don't get hung up on things like this in your progress...

But simply:



background = &image.load_image("Pics/background.jpg");


I already tried that, which gives me even more errors

1. undefined reference to 'Image::Image()'2. undefined reference to 'Image::load_image(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'3. undefined reference to 'Image::load_image(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'


I marked the errors in the code:

#include <string>#include <stdlib.h>#include <iostream>#include "SDL/SDL.h"#include "game.h"#include "images.h"using namespace std;//attributes of the screenconst int SCREEN_BPP = 32;//surfaces to be usedSDL_Surface *cross = NULL;SDL_Surface *background = NULL;SDL_Surface *screen = NULL;Game::Game(){	initiate();		Image *image = new Image(); <<<<ERROR 1	//create background	setBackground(*image);}bool Game::initiate(){	//Initialize all SDL subsystems 	if(SDL_Init(SDL_INIT_EVERYTHING) == -1) 	{ 		return false; 	}	//set up the screen	screen = SDL_SetVideoMode(600, 400, SCREEN_BPP, SDL_SWSURFACE);	//If there was an error in setting up the screen 	if( screen == NULL )	{ 		return false; 	}		//Set the window caption 	SDL_WM_SetCaption("Residual Hope", NULL); 	return true;}int Game::setBackground(Image image){	background = &image.load_image("Pics/background.jpg"); <<<<ERROR 2	cross = &image.load_image("Pics/cross.jpg");           <<<<ERROR 3	image.apply_surface(100, 100, cross, background);	//If there was a problem in loading the image    if(background == NULL)    {    	cout << "Error loading image";        return 1;        }}	


I thought images.h might help

#ifndef IMAGES_H_#define IMAGES_H_#include <string.h>using namespace std;class Image{	public:	Image(void);	SDL_Surface load_image(string filename);	void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination);};#endif /*IMAGES_H_*/
DevFred
DevFred
Quote:
Original post by Telastyncode]
background = &image.load_image("Pics/background.jpg");

But load_image already returns a pointer. (And I don't believe you are allowed to take the address from that, because what the function returns here is not an lvalue.)

Quote:
Game::Game(){	initiate();		Image *image = new Image();	//create background	setBackground(*image);}Game::~Game(void){}bool Game::initiate() {...}

Why do you call an initiate function from the constructor? The constructor itself should initiate the object. initiate returns are bool, why are you ignoring the return value?

Why are you reserving the background from the heap, pointed to by a local variable? Who is responsible for cleaning that memory up? Why do you have an empty destructor?

Why do you have (void) instead of () as the empty parameter list? This isn't C.

Quote:
int Game::setBackground(Image image)

Are you sure you want to pass image by value?
CompEngRH
CompEngRH
It appears that load_image isn't a member of your class Image.
SDL_Surface *load_image(string filename)

pi_equals_3
pi_equals_3
SDL_Surface *Image::load_image(string filename){	//image to be loaded	SDL_Surface* loadedImage  = NULL;	//optimized image	SDL_Surface* optimizedImage = NULL;	//load the image	loadedImage = IMG_Load(filename.c_str());	//if the image loaded	if(loadedImage != NULL)	{		//create optimized image		optimizedImage = SDL_DisplayFormat(loadedImage);		//free the old image		SDL_FreeSurface(loadedImage);	}	//return the optimized image	return optimizedImage;}


You're missing "Image::" before your load_image definition. That's why you got the undefined reference errors.
Aardvajk
Aardvajk
[EDIT: pi_equals_3 ninjas me [smile]]

Maybe this is a typo on the OP's part, but this from images.cpp

SDL_Surface *load_image(string filename){	//image to be loaded	SDL_Surface* loadedImage  = NULL;


should be

SDL_Surface *Image::load_image(string filename){	//image to be loaded	SDL_Surface* loadedImage  = NULL;


If that isn't a typo, it would explain the undefined reference errors. You declare a method Image::load_image() but then define a free function load_image(). These have no relation to each other, therefore the declared Image::load_image() method is called but not defined.
Telastyn
Telastyn
Quote:
Original post by DevFred
Quote:
Original post by Telastyncode]
background = &image.load_image("Pics/background.jpg");

But load_image already returns a pointer. (And I don't believe you are allowed to take the address from that, because what the function returns here is not an lvalue.)


Ah yes, I've not used SDL, just going off the error message; and you're right about the syntax. Kinda pushed C++ minutiae to a dusty corner of my mind.
woody1987
woody1987
Thanks pi_equals_3 that solved the error! Cant believe i missed that

Topic Locked

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

Sign in to reply to this topic.