Jump to content

No match for operator=

- - - - -

  • Please log in to reply
2 replies to this topic

#1
matio

matio

    Learning Programmer

  • Members
  • PipPipPip
  • 51 posts
Hi everybody, I'm trying to start game programming with C++ & SDL, I'm making a small library with a state machine in it:

// statemachine.h

#ifndef STATEMACHINE_H

#define STATEMACHINE_H


#include <vector>

#include "SDL/SDL.h"

#include "screen.h"


class SFrameState

{

protected:

    SFrameScreen screen;

public:

    SFrameState(){}

    SFrameState(SFrameScreen _screen);

    SFrameState(const SFrameState &rhs) {}

    const SFrameState operator=(const SFrameState &rhs);

    virtual void handleInput(const SDL_Event event);

    virtual void render();

    virtual void logic();

};


class SFrameStateMachine

{

private:

    SFrameScreen screen;

    std::vector<SFrameState> states;

public:

    SFrameStateMachine(SFrameScreen &_screen, std::vector<SFrameState> _states);

};


#endif



//statemachine.cpp

#include "statemachine.h"


SFrameStateMachine::SFrameStateMachine(SFrameScreen &_screen, std::vector<SFrameState> _states)

{

    screen = _screen;

    states = _states;

}


const SFrameState SFrameState::operator =(const SFrameState &rhs)

{

    return *this;

}

The machine will take in a vector of states and use this to switch between them, here's my test program:

#include <iostream>

#include "sdlframe.h"


class StateMain : SFrameState

{

private:

    SFrameTimer timer;

    SFrameImage image;

    int x,speed;

public:

    StateMain();

    void handleInput(SDL_Event event);

    void render();

    void logic();

};


StateMain::StateMain()

{

    x = 0;

    speed = 100;

//    image("dot.png");

    image = SFrameImage("dot.png");

}


void StateMain::handleInput(SDL_Event event)

{

    while(SDL_PollEvent(&event))

    {

        if(event.type == SDL_QUIT)

        {

        }

    }

}


void StateMain::render()

{

    screen.fill(255,255,255);

    screen.blit(x,120,image,NULL);

}


void StateMain::logic()

{

    float distance = timer.tick()/1000000.;

    distance *= speed;

    x += distance;

}


int main()

{

    SFrameInitEverything();


    SFrameScreen screen(640,480,32);

    SFrameImage image("dot.bmp");

    SDL_Event event;

    SFrameTimer t;

    float x = 0;

    int speed = 100;

    bool quit = false;


    while(quit == false)

    {

        float distance = t.tick()/1000000.;

        distance *= speed;

        x += distance;


        while(SDL_PollEvent(&event))

        {

            if(event.type == SDL_QUIT)

            {

                quit = true;

            }

        }

        screen.fill(255,255,255);

        screen.blit(x,120,image,NULL);

        if(screen.flip() == false)

        {

            quit = true;

        }

        std::cout << distance << std::endl;

        std::cout << x << std::endl;

    }

    SFrameCloseEverything();

    return 1;

}


When I compile this the compiler outputs:

/home/matthew/Documents/Programming/C++/SDL/framework/demo/test.cpp:22: error: no match for ‘operator=’ in ‘((StateMain*)this)->StateMain::image = SFrameImage(std::basic_string<char, std::char_traits<char>, std::allocator<char> >(((const char*)"dot.png"), ((const std::allocator<char>&)((const std::allocator<char>*)(& std::allocator<char>())))))’


Why is this? I think it is something to do with the way I defined operator new:

// Image class inherits Surface

const SFrameSurface SFrameSurface::operator =(SFrameSurface &rhs)

{

    width = rhs.width;

    height = rhs.height;

    surface = rhs.surface;

    return *this;

}


const SFrameImage& SFrameImage::operator =(std::string filename)

{

    SDL_Surface* loadedImage = NULL;

    SDL_Surface* optimizedImage = NULL;

    loadedImage = IMG_Load( filename.c_str() );

    if( loadedImage != NULL )

    {

        optimizedImage = SDL_DisplayFormat( loadedImage );

        SDL_FreeSurface( loadedImage );

    }else{

        throw std::string("Image could not be loaded (does is exist?)");

    }

    surface = optimizedImage;

    return *this;

}


void *SFrameImage::operator new(size_t size)

{

    void *p;

    p = malloc(size);

    return p;

}


void SFrameImage::operator delete(void *p)

{

    free(p);

}


Sorry for this badly written post!

Attached Files



#2
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
Firstly why are you redefining operator new?

secondly:

image = SFrameImage("dot.png"); 
is a little ineffcient, this says, create a anonymous SFrameImage, then copy it to image. What you probably want is to create an initlisation list for it.
StateMain::StateMain () : image("dog.png") { }
This bypasses your problem.

In regard to your problem, I guess the default assignment operator isn't being generated because you've defined your own, but used a different data type. Although it's late so best look that up.

#3
manux

manux

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 234 posts
But you wrote
[COLOR=#000000][COLOR=#0000bb]SFrameImage[/COLOR][COLOR=#007700]& [/COLOR][COLOR=#0000bb]SFrameImage[/COLOR][COLOR=#007700]::[/COLOR][COLOR=#0000bb]operator [/COLOR][COLOR=#007700]=([/COLOR][COLOR=#0000bb]std[/COLOR][COLOR=#007700]::[/COLOR][COLOR=#0000bb]string filename[/COLOR][COLOR=#007700]) [/COLOR][/COLOR]
Yet you are assigning an SFrameImage, not a std::string:
[COLOR=#000000][COLOR=#0000bb]image [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]SFrameImage[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]"dot.png"[/COLOR][COLOR=#007700]); [/COLOR][/COLOR]
Maybe I'm confused, but shouldn't you write:
[COLOR=#000000][COLOR=#0000bb]image [/COLOR][COLOR=#007700]= [/COLOR][/COLOR][COLOR=#000000][COLOR=#dd0000]"[/COLOR][/COLOR][COLOR=#000000][COLOR=#dd0000]dot.png"[/COLOR][COLOR=#007700]; [/COLOR][/COLOR]





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users