What does this error mean?

Started by
14 comments, last by darookie 19 years, 6 months ago
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]
Advertisement
Another thing,will my run funcion move my image?
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]
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.
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One
I've nerver encounterd an error like that before I know the basics of c++ do I need to know more??
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( )).

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.
I don't even know where to start correcting this code...
Now I get a cookie!
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".
copy and paste this into a *.cpp file and compare it to your original version.

/* ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo                                                                oooo [AXENATION 10-01-04]                                           oooo PS...this may not work....i havent tried to compile it.        oooo                                                                oooo ---- Instructions ----                                         oooo                                                                oooo compile code and run exe.                                      oooo then press left mouse button to blit some image to the screen. oooo then compare this code to your original code. hopefully you'll oooo understand why i changed and adding some code.                 oooo                                                                ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo */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]
Now I get a cookie!

This topic is closed to new replies.

Advertisement