Sign in to follow this  
MPG

What does this error mean?

Recommended Posts

error C2676: binary '+' : 'SDL_Rect' does not define this operator or a conversion to a type acceptable to the predefined operator
ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif
#include "SDL.h"
#include <stdlib.h>

;

void run()
{
   float start_time;
   start_time = SDL_GetTicks();

   //  loop 

   while(true)
   { 
      float dtime;
      float end_time;
      float elapsed_time;

      float vvlosity;
      vvlosity = 5;

      SDL_Rect dest;  
      dest.x = 2;  
      dest.y = 2;  

      end_time = SDL_GetTicks();
    
      // now we have how much time passed in the last loop 
      elapsed_time = end_time - start_time;
  
      // here's the new code, dtime must be a floating point number 
      dtime = elapsed_time / 1000;

      // Get new start time 
      start_time = SDL_GetTicks(); 
      dest=dest + dtime *vvlosity;
     // end loop 
   }



   int main( int argc, char* argv[] )
   {
      ;

      SDL_Surface * screen = SDL_SetVideoMode( 800,600, 24, SDL_ANYFORMAT|SDL_DOUBLEBUF );  
      SDL_Surface * img = SDL_LoadBMP( "filename.bmp" );   

      SDL_Rect dest;  
      dest.x = 2;  
      dest.y = 2;  

      SDL_Event event;
      SDL_Event a;
      SDL_Event b;
      SDL_Event Event;

      SDL_Rect coord;
      coord.x = 200;
      coord.y = 500;

      SDL_Surface * tdisplay = SDL_SetVideoMode( 800,600,24,SDL_ANYFORMAT|SDL_DOUBLEBUF );
      SDL_Surface * imi = SDL_LoadBMP( "ad.bmp" );
      SDL_Surface * bckg = SDL_LoadBMP ( "bg.bmp" );
               
      SDL_Flip( screen );
      Uint32 now;

      ;

      while(true) 
      {		
         SDL_BlitSurface(img,NULL,screen, &dest);		
         SDL_PollEvent(&event);		
         if(event.type == SDL_MOUSEBUTTONDOWN) 
         {			
            if(event.button.button == SDL_BUTTON_LEFT) 
            {				
               SDL_BlitSurface(imi,NULL,tdisplay,&coord);		               SDL_Flip(tdisplay);
               ;
            }
         }	
      }

      if(event.type == SDL_QUIT) 
      { 
         return 0; 
      }
  
      SDL_Event up;
      while(true)
      {
         if(up.type == SDL_KEYDOWN)
         {
            if(up.key.keysym.sym == SDLK_SPACE)
            {
               run();
            }
         }
      }
   }

   atexit(SDL_Quit);
   return run();
   ;
}

Another thing,will my run funcion move my image
Edit - Code reformatted and semicolons moved. No other corrections done. [Edited by - Fruny on October 1, 2004 2:55:00 AM]

Share this post


Link to post
Share on other sites
Your error is here:
dest=dest + dtime * vvlosity;

You cannot add anything to an SDL_Rect struct unless you define such operator.
You are trying to modify a specific member of the rect, not the rect itself. So you have to be explicit about it and tell the compiler, which component you want to modify, e.g.

dest.x += dtime * velocity;

Also consider to revise your code, as returning a void function's return value (that is essentially 'void'!) in your main function, which returns 'int' is no good. And it will lead to further errors.

[edit]
Another thing: please re-structure your code. It looks awfull, esp. because your semicolons are misplaced. This makes it hard to read and look at.
[/edit]

Share this post


Link to post
Share on other sites
Why don't you go back, and take some time to learn C++. You seem to just be fixing the errors exactly how the compiler tells you to. Sometimes the compiler reads it wrong, and you should learn how to read and correct the code by hand.

Share this post


Link to post
Share on other sites
I've nerver encounterd an error like that before I know the basics of c++ do I need to know more??

Share this post


Link to post
Share on other sites
Quote:
Original post by MPG
I've nerver encounterd an error like that before I know the basics of c++ do I need to know more??

Yes.
You need to know much more. Tracking down errors as the one you are getting in your program is simple if you know C++ good enough.
If the compiler tells you about a binary operator that is not available, you most likely have forgotten something. Like the ".x" or whatever member of SDL_Rect you wanted to modify.

The overall structure of your code also suggests that you are missing some basic concepts (like not using a void function as return value of int main( )).

Share this post


Link to post
Share on other sites
I didnt want to put return run(); but it gave me this:
error C2562: 'run' : 'void' function returning a value
see declaration of 'run'.
The last error is still there except its on the line where ";}"is and idf I dont put semi-colon before the staatment I get an error.

Share this post


Link to post
Share on other sites
My god, is that indentation caused by the transfer from code to forums or do you acually write like that? Putting the semicolons before the following statements is NOT the standard approach, eg. ";dtime = elapsed_time / 1000".

Share this post


Link to post
Share on other sites
copy and paste this into a *.cpp file and compare it to your original version.



/* ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oo oo
oo [AXENATION 10-01-04] oo
oo PS...this may not work....i havent tried to compile it. oo
oo oo
oo ---- Instructions ---- oo
oo oo
oo compile code and run exe. oo
oo then press left mouse button to blit some image to the screen. oo
oo then compare this code to your original code. hopefully you'll oo
oo understand why i changed and adding some code. oo
oo oo
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo */


ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif
#include "SDL.h"
#include <stdlib.h>

// [AXENATION 10-01-04]
// the while loop in this function has no way of breaking.
// so you should probably rewrite it.
void run(void)
{
float start_time;
start_time = SDL_GetTicks();

// loop
while(true){
float dtime;
float end_time;
float elapsed_time;
float vvlosity;
vvlosity = 5;
SDL_Rect dest;
dest.x = 2;
dest.y = 2;
end_time = SDL_GetTicks();

// now we have how much time passed in the last loop
elapsed_time = end_time - start_time;

// here's the new code, dtime must be a floating point number
dtime = elapsed_time / 1000;

// Get new start time
start_time = SDL_GetTicks();
dest.x = dest.x + dtime *vvlosity;
dest.y = dest.y + dtime *vvlosity;


// end loop
}
}

int main( int argc, char* argv[] )
{
// [AXENATION 10-0104]
// This code will crash as you are making SDL library calls
// before initiating SDL via the SDL_Init() funtion.
// (adding in correct code for intiation of the SDL library)
if(SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, "-={ error }=- : SDL_Init()\n");
return 0;
}
atexit(SDLQuit);


SDL_Surface * screen = SDL_SetVideoMode( 800,600, 24, SDL_ANYFORMAT|SDL_DOUBLEBUF );
SDL_Surface * img = SDL_LoadBMP( "filename.bmp" );

SDL_Rect dest;
dest.x = 2;
dest.y = 2;

SDL_Event event;

// [AXENATION 10-01-04]
// these three SDL_Events are never used thus im removing them.
//SDL_Event a;
//SDL_Event b;
//SDL_Event Event;

SDL_Rect coord;
coord.x = 200;
coord.y = 500;

// [AXENATION 10-01-04]
// commenting this out as you have already set the video mode
//SDL_Surface * tdisplay = SDL_SetVideoMode( 800,600,24,SDL_ANYFORMAT|SDL_DOUBLEBUF );

SDL_Surface * imi = SDL_LoadBMP( "ad.bmp" );
SDL_Surface * bckg = SDL_LoadBMP ( "bg.bmp" );

// [AXENATION 10-01-04]
// this shouldnt really be here as
// you havent blitted anything to the display surface (screen)
// yet via SDL_BlitSurface().
// (commenting this out)
//SDL_Flip( screen );

Uint32 now;


while(true) {
SDL_BlitSurface(img,NULL,screen, &dest);
SDL_PollEvent(&event);
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
// [AXENATION 10-01-04]
// 1. do you realize that this will only blit the minage to the
// display surface when the left mouse button is pressed.
// 2. i removed the tdisplay variable (read above) so im
// recoding this to use "screen".
//SDL_BlitSurface(imi,NULL,tdisplay,&coord);
//SDL_Flip(tdisplay);

// [Axenation 10-01-04]
// SDL_GetMouseState(x,y) retrieves the current
// x and y positions of the mouse.

// [Axenation 10-01-04]
// putting mouse cordinates into SDL_Rect "coord"
// structure and then using that rect as the last
// parameter in SDL_BlitSurface() will blit the
// surface at the mouse coordinates.
SDL_GetMouseState(&coord.x, &coord.y);

SDL_BlitSurface(imi,NULL,screen,&coord);
SDL_Flip(screen);
}
}
if(event.type == SDL_QUIT)
{
return 0;
}

// [AXENATION 10-01-04]
// what is this here for? You only need to use
// one SDL_Event structure (hence the one defined above)
// (im commenting this out)
//SDL_Event up;
//while(true){
// if(up.type == SDL_KEYDOWN)
// {
// if(up.key.keysym.sym == SDLK_SPACE)
// {
// run();
// }
// }
//}

// [AXENATION 10-0104]
// and this is more likely the correct code.
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_SPACE)
{
// [AXENATION 10-01-04]
// commenting this out as it enters an infinite loop
//run();
}
}
}

// [AXENATION 10-01-04]
// What is atexit() doing here?
// you are SUPPOSED to call "atexit(SDL_Quit)"
// immediatly after you initiate SDL via SDL_Init()
// (im commenting this out)
//atexit(SDL_Quit);
//return run();

// [AXENATION 10-0104]
// and this is more likely the correct code.
return 0;
}






[edited to respond to Unwise owl]
The awful indentation is, im guessing, due to random copying and pasting.

[edited - fixed mistake]

Share this post


Link to post
Share on other sites
Quote:
Original post by Unwise owl
My god, is that indentation caused by the transfer from code to forums or do you acually write like that? Putting the semicolons before the following statements is NOT the standard approach, eg. ";dtime = elapsed_time / 1000".


The code used a mix of tabs and spaces to indent. The OP's editor probably used a different tab width than your web browser. I fixed things up.

Share this post


Link to post
Share on other sites
Sorry about the semi-colons if I dont put them before insteead of after I get an error.Um after the int mai on the next line I get an error that says:SDL_main' : local function definitions are illegal. I dont get why it's on a "{".CODE UPDATE

error C2676: binary '+' : 'SDL_Rect' does not define this operator or a conversion to a type acceptable to the predefined operator
code update
Since I can't edit my originaal post i'm doing it here

ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif
#include "SDL.h"
#include <stdlib.h>

;

void run()
{
float start_time;
start_time = SDL_GetTicks();
int i;
i=5
// loop

while(i=5)
{
float dtime;
float end_time;
float elapsed_time;

float vvlosity;
vvlosity = 5;

SDL_Rect dest;
dest.x = 2;
dest.y = 2;

end_time = SDL_GetTicks();

// now we have how much time passed in the last loop
elapsed_time = end_time - start_time;

// here's the new code, dtime must be a floating point number
dtime = elapsed_time / 1000;

// Get new start time
start_time = SDL_GetTicks();
dest.x=dest.x + dtime *vvlosity;
dest.y = dest.y + dtime *vvlosity;
// end loop
}



int main( int argc, char* argv[] )
{
//initialize systems
SDL_Init ( SDL_INIT_VIDEO ) ;
//set our at exit function
atexit ( SDL_Quit ) ;


SDL_Surface * screen = SDL_SetVideoMode( 800,600, 24, SDL_ANYFORMAT|SDL_DOUBLEBUF );
SDL_Surface * img = SDL_LoadBMP( "filename.bmp" );

SDL_Rect dest;
dest.x = 2;
dest.y = 2;

SDL_Event event;
SDL_Event a;
SDL_Event b;
SDL_Event Event;

SDL_Rect coord;
coord.x = 200;
coord.y = 500;

SDL_Surface * tdisplay = SDL_SetVideoMode( 800,600,24,SDL_ANYFORMAT|SDL_DOUBLEBUF );
SDL_Surface * imi = SDL_LoadBMP( "ad.bmp" );
SDL_Surface * bckg = SDL_LoadBMP ( "bg.bmp" );


Uint32 now;

;

while(true)
{
SDL_BlitSurface(img,NULL,screen, &dest);
SDL_PollEvent(&event);
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
SDL_GetMouseState(&coord.x, &coord.y);


SDL_BlitSurface(imi,NULL,tdisplay,&coord); SDL_Flip(tdisplay);
;
}
}
}

if(event.type == SDL_QUIT)
{
return 0;
}

SDL_Event up;
while(true)
{
if(up.type == SDL_KEYDOWN)
{
if(up.key.keysym.sym == SDLK_SPACE)
{
run();
}
}
}
}

atexit(SDL_Quit);
return run();
;
}






[Edited by - MPG on October 1, 2004 10:34:27 AM]

Share this post


Link to post
Share on other sites
[edit: never mind]
For heaven's sake - get a decent editor!
Your run() function misses the closing '}'.
Also why are you spreading completely useless semicolons all over your code?


#ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif
#include "SDL.h"
#include <stdlib.h>

; // <--- what is this semicolon for???

void run()
{
float start_time;
start_time = SDL_GetTicks();

// loop

while(true) // <--- ????
{
float dtime;
float end_time;
float elapsed_time;

float vvlosity;
vvlosity = 5;

SDL_Rect dest;
dest.x = 2;
dest.y = 2;

end_time = SDL_GetTicks();

// now we have how much time passed in the last loop
elapsed_time = end_time - start_time;

// here's the new code, dtime must be a floating point number
dtime = elapsed_time / 1000;

// Get new start time
start_time = SDL_GetTicks();
dest=dest + dtime *vvlosity;
// end loop
}

} // <--- end run() function


int main( int argc, char* argv[] )
{
; // <--- ????

// AFAIK you need to call SDL_Init() first...

SDL_Surface * screen = SDL_SetVideoMode( 800,600, 24, SDL_ANYFORMAT|SDL_DOUBLEBUF );
SDL_Surface * img = SDL_LoadBMP( "filename.bmp" );

SDL_Rect dest;
dest.x = 2;
dest.y = 2;

SDL_Event event;
SDL_Event a;
SDL_Event b;
SDL_Event Event;

SDL_Rect coord;
coord.x = 200;
coord.y = 500;

// I didn't use SDL for years, but this is certainly wrong!
// Why do you chenge the video mode again?
SDL_Surface * tdisplay = SDL_SetVideoMode( 800,600,24,SDL_ANYFORMAT|SDL_DOUBLEBUF );
SDL_Surface * imi = SDL_LoadBMP( "ad.bmp" );
SDL_Surface * bckg = SDL_LoadBMP ( "bg.bmp" );

SDL_Flip( screen );
Uint32 now;

; // <--- ????

while(true) // <--- ????
{
SDL_BlitSurface(img,NULL,screen, &dest);
SDL_PollEvent(&event);
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
SDL_BlitSurface(imi,NULL,tdisplay,&coord); SDL_Flip(tdisplay);
; // <--- ????
}
}
}

if(event.type == SDL_QUIT) // <--- ????
{
return 0;
}

SDL_Event up;
while(true) // <--- ????
{
if(up.type == SDL_KEYDOWN)
{
if(up.key.keysym.sym == SDLK_SPACE)
{
run();
}
}
}
}

atexit(SDL_Quit); // <--- ???? you already did that...
return run();
; // <--- ????
}



No offense, but do you even have the slightest idea of what your code does? You have so many infinitive loops that just make no sense at all (marked by ????). And the program doesn't have any reasonable structure (ie. init SDL -> load surfaces -> event-loop -> exit).

Sorry to say that, but it seems you have absolutely no clue of what you are doing. Try to start from scratch by just writing a clean event loop.

[Edited by - darookie on October 1, 2004 11:59:43 AM]

Share this post


Link to post
Share on other sites
Ok I pput what neede to be in a into one big loop. In my run function would i still need a loop?


EDIT: darookie, I don't know how thoose semi-colons got there ut,there not in my editor.

Share this post


Link to post
Share on other sites
Quote:
Original post by MPG
Ok I pput what neede to be in a into one big loop. In my run function would i still need a loop?

I'll post a working version in a couple of minutes. So stay tuned [wink].
Quote:

EDIT: darookie, I don't know how thoose semi-colons got there ut,there not in my editor.

Maybe some copy 'n paste issue... Not your fault then obviously.

PS: Sorry for bitching about your code, it just looked too confusing.

Share this post


Link to post
Share on other sites
First of all THANKS MPG!
Since I didn't have SDL installed on my Windows box, I quickly setup my laptop for SDL (it runs Linux). Now I seriously consider porting an old game of mine from DOS to Linux using SDL [smile].

Oh, and here's some code for you:


#ifdef WIN32
# pragma comment(lib, "SDL.lib")
# pragma comment(lib, "SDLmain.lib")
#endif
#include <SDL/SDL.h> // <-- you might need to change that into #include "sdl.h"
#include <cstdio>
#include <cstdlib>

// window setup - change to 800 and 600 if you like
static const int WINDOW_WIDTH = 640;
static const int WINDOW_HEIGHT= 480;

// Initialise SDL
// - returns boolean result: true if succeeded false
// otherwise
bool Initialise() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Could not initialize SDL: %s\n",
SDL_GetError());
return false;
}
atexit(SDL_Quit);
return true;
}

// Setup SDL video mode
// - returns Screen surface or NULL pointer on failure
SDL_Surface *SetupVideoMode() {
SDL_Surface *screen = 0;
screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 16, SDL_SWSURFACE|SDL_ANYFORMAT|SDL_DOUBLEBUF);
if( !screen ) {
fprintf(stderr, "Couldn't create a surface: %s\n",
SDL_GetError());
}
return screen;
}

// Main program entry point
int main(int argc, char *argv[]) {
// step 1) - initialisation
if (!Initialise()) {
return -1;
}
// step 2) - setup video mode
SDL_Surface *screen = SetupVideoMode();
if (!screen) {
return -2;
}
// step 3) - load resources
SDL_Surface * background; // this is my background image, it matches the window size
SDL_Surface * sprite; // some small sprite image

background = SDL_LoadBMP("bkgnd.bmp");
sprites = SDL_LoadBMP("sprites.bmp");
if (!background || !sprites) {
fprintf(stderr, "Failed to load image: %s\n",
SDL_GetError());
return -3;
}

// the position of the sprite on screen
SDL_Rect spritePosition;
spritePosition.x = 0;
spritePosition.y = WINDOW_HEIGHT / 2; // place it somewhere in the middle of the window

// step 4) - event loop
SDL_Event event;
bool exitRequested = false; // this flag terminates the loop when set to "true"

while (!exitRequested) {
// draw background and sprite
SDL_BlitSurface(background, 0, screen, 0);
SDL_BlitSurface(sprites, 0, screen, &spritePosition);

// check for events
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
exitRequested = true;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
// escape key exits
case SDLK_ESCAPE:
exitRequested = true;
break;
// hitting left cursor moves the prite left
case SDLK_LEFT:
if (spritePosition.x > 0)
spritePosition.x += 4;
break;
// same as above but in the other direction
case SDLK_RIGHT:
if (spritePosition.x < WINDOW_WIDTH-60)
spritePosition.x += 4;
break;
default:
break;
}
break;
}
}
// update screen content
SDL_Flip(screen);
}

return 0;
}




Have fun with that,
Pat.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this