[SDL] 'operator ' is ambiguous

Started by
8 comments, last by GameDev.net 17 years, 6 months ago
This is the code in which I am having errors

       //If the timer is running
        if( running == true )
        {
            //The timer's time as a string
            std::stringstream time;
        
            //Convert the timer's time to a string
            time << "Timer: " << SDL_GetTicks() - start;
        
            //Render the time surface
            seconds = TTF_RenderText_Solid( font, time.str().c_str(), textColor );

//rest of the code            

I am getting error as error C2593: 'operator <<' is ambiguous for this line time << "Timer: " << SDL_GetTicks() - start;
Advertisement
have you tried using an std::string instead of an std::stringstream?
I've never used the std::stringstream type, but I'm certain that a std::string supports using the '<<' operator...I'll try to run some tests...only been learning C++(used C for a long time instead) for 4-5 months now, so I'm not sure what it means by it's ambiguous...and being a 10th grade student, I can't seem to find that word anywhere in the dictionary in my head...so I'm off to google...

-Wynter Woods(aka Zerotri)

EDIT: okay, so I'm no expert at this, but stringstreams aren't wanting to work for me(although because of linker issues, not compiler ones). personally, I would stick with normal std::strings. and the only changes I see that you'd need to do are the declaration of 'time', and change 'time.str().c_str()' to 'time.c_str()'...but your knowledge of c++ most likely surpasses mine...I'm just giving my opinion. hope it helps!
Quote:Original post by TEUTON
I am getting error as error C2593: 'operator <<' is ambiguous


Didn't the compiler tell you what were the different possibilities?

Quote:Original post by zerotri
I've never used the std::stringstream type, but I'm certain that a std::string supports using the '<<' operator...


No, it does not. That's precisely what stringstream is for.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
Quote:Original post by zerotri
I've never used the std::stringstream type, but I'm certain that a std::string supports using the '<<' operator...


No, it does not. That's precisely what stringstream is for.


ah, I see. sounds like I need to sharpen my knowledge of the c++ STL some more. thanks for that bit of info, I'll try and remember that next time I try to use '<<' on an 'std::string' and the compiler squawks at me.

-Wynter Woods(aka Zerotri)
your code compiles clean with gcc.

the first thing i'd try in your case would be putting SDL_GetTicks()-start in parentheses. it souldn't really matter I know, but you shouldn't be getitng no error either.

are you sure about the "... - start" bit though? is 'start' initialised at application startup? then it's close to zero and SDL_GetTicks()-start is close to SDL_GetTicks().

Quote:Original post by Fruny
Didn't the compiler tell you what were the different possibilities?


This is the complete source

//The headers#include "SDL/SDL.h"#include "SDL/SDL_image.h"#include "SDL/SDL_ttf.h"#include <string>#include <sstream>//Screen attributesconst int SCREEN_WIDTH = 640;const int SCREEN_HEIGHT = 480;const int SCREEN_BPP = 32;//The surfacesSDL_Surface *background = NULL;SDL_Surface *startStop = NULL;SDL_Surface *seconds = NULL;SDL_Surface *screen = NULL;//The event structureSDL_Event event;//The fontTTF_Font *font = NULL;//The color of the fontSDL_Color textColor = { 0xF0, 0xFF, 0xF0 };SDL_Surface *load_image( std::string filename ) {    //The image that's loaded    SDL_Surface* loadedImage = NULL;        //The optimized surface that will be used    SDL_Surface* optimizedImage = NULL;        //Load the image    loadedImage = IMG_Load( filename.c_str() );        //If the image loaded    if( loadedImage != NULL )    {        //Create an optimized surface        optimizedImage = SDL_DisplayFormat( loadedImage );                //Free the old surface        SDL_FreeSurface( loadedImage );                //If the surface was optimized        if( optimizedImage != NULL )        {            //Color key surface            SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );        }    }        //Return the optimized surface    return optimizedImage;}void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL ){    //Holds offsets    SDL_Rect offset;        //Get offsets    offset.x = x;    offset.y = y;        //Blit    SDL_BlitSurface( source, clip, destination, &offset );}bool init(){    //Initialize all SDL subsystems    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )    {        return false;        }        //Set up the screen    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );        //If there was in error in setting up the screen    if( screen == NULL )    {        return false;        }        //Initialize SDL_ttf    if( TTF_Init() == -1 )    {        return false;        }        //Set the window caption    SDL_WM_SetCaption( "Timer Test", NULL );        //If everything initialized fine    return true;}bool load_files(){    //Load the background image    background = load_image( "background3.png" );        //Open the font    font = TTF_OpenFont( "lazy.ttf", 36 );        //If there was a problem in loading the background    if( background == NULL )    {        return false;        }        //If there was an error in loading the font    if( font == NULL )    {        return false;    }        //If everything loaded fine    return true;}void clean_up(){    //Free the surfaces    SDL_FreeSurface( background );    SDL_FreeSurface( startStop );    //Close the font    TTF_CloseFont( font );        //Quit SDL_ttf    TTF_Quit();        //Quit SDL    SDL_Quit();}int main( int argc, char* args[] ){    //Quit flag    bool quit = false;        //The timer starting time    Uint32 start = 0;        //The timer start/stop flag    bool running = true;        //Initialize    if( init() == false )    {        return 1;    }        //Load the files    if( load_files() == false )    {        return 1;    }        //Generate the message surface    startStop = TTF_RenderText_Solid( font, "Press S to start or stop the timer", textColor );        //Start the timer    start = SDL_GetTicks();        //While the user hasn't quit    while( quit == false )    {        //While there's an event to handle        while( SDL_PollEvent( &event ) )        {            //If a key was pressed            if( event.type == SDL_KEYDOWN )            {                //If s was pressed                if( event.key.keysym.sym == SDLK_s )                {                    //If the timer is running                    if( running == true )                    {                        //Stop the timer                        running = false;                        start = 0;                      }                    else                    {                        //Start the timer                        running = true;                        start = SDL_GetTicks();                      }                }            }                        //If the user has Xed out the window            else if( event.type == SDL_QUIT )            {                //Quit the program                quit = true;            }        }                //Apply the background        apply_surface( 0, 0, background, screen );                //Apply the message        apply_surface( ( SCREEN_WIDTH - startStop->w ) / 2, 200, startStop, screen );                //If the timer is running        if( running == true )        {            //The timer's time as a string            std::stringstream time;                    //Convert the timer's time to a string            time << "Timer: " << SDL_GetTicks() - start;                    //Render the time surface            seconds = TTF_RenderText_Solid( font, time.str().c_str(), textColor );                        //Apply the time surface            apply_surface( ( SCREEN_WIDTH - seconds->w ) / 2, 50, seconds, screen );                    //Free the time surface            SDL_FreeSurface( seconds );        }                //Update the screen        if( SDL_Flip( screen ) == -1 )        {            return 1;            }    }            //Clean up    clean_up();        return 0;    }


There's only 1 error
error C2593: 'operator <<' is ambiguous
Quote:Original post by artm
your code compiles clean with gcc.

I am using VC6.0, SP5

Quote:Original post by artmthe first thing i'd try in your case would be putting SDL_GetTicks()-start in parentheses.


I did tried that but still the same error.

Visual studio 6.0 doesn't follow the c++ standard (since it was released before there was a standard), i would suggest that you upgrade to a modern compiler.
VS.Net 2005 EE should be avaliable for free from microsoft and has a much better compiler.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Definately you should update compilers. MSVC 8 Express is free so there is no reason not to.

If you want a quick fix, you could try explicitly casting it to int:
time << "Timer: " << (int)(SDL_GetTicks() - start);


If that does not work, you could try assigning it separately:
int temp = SDL_GetTicks() - start;time << "Timer: " << temp;


If that does not work, I have no idea what could!
Thanks

Casting did the trick. SDL_GetTicks() is returning Uint32. So, I will need to cast it.

This topic is closed to new replies.

Advertisement