Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #9142 > unrolled thread

Operator overloading in memberclass.

Started byMarz <marzkrishna@gmail.com>
First post2011-08-21 15:26 -0700
Last post2011-08-22 13:32 -0400
Articles 11 — 6 participants

Back to article view | Back to comp.lang.c++


Contents

  Operator overloading in memberclass. Marz <marzkrishna@gmail.com> - 2011-08-21 15:26 -0700
    Re: Operator overloading in memberclass. Ian Collins <ian-news@hotmail.com> - 2011-08-22 11:42 +1200
      Re: Operator overloading in memberclass. Marz <marzkrishna@gmail.com> - 2011-08-21 20:17 -0700
        Re: Operator overloading in memberclass. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-08-22 04:12 -0700
          Re: Operator overloading in memberclass. Marz <marzkrishna@gmail.com> - 2011-08-22 10:15 -0700
            Re: Operator overloading in memberclass. Nobody <nobody@nowhere.com> - 2011-08-22 20:00 +0100
              Re: Operator overloading in memberclass. Marz <marzkrishna@gmail.com> - 2011-08-22 14:28 -0700
      Re: Operator overloading in memberclass. "Balog Pal" <pasa@lib.hu> - 2011-08-22 13:25 +0200
        Re: Operator overloading in memberclass. Marz <marzkrishna@gmail.com> - 2011-08-22 10:19 -0700
          Re: Operator overloading in memberclass. "Balog Pal" <pasa@lib.hu> - 2011-08-22 22:42 +0200
    Re: Operator overloading in memberclass. Victor Bazarov <v.bazarov@comcast.invalid> - 2011-08-22 13:32 -0400

#9142 — Operator overloading in memberclass.

FromMarz <marzkrishna@gmail.com>
Date2011-08-21 15:26 -0700
SubjectOperator overloading in memberclass.
Message-ID<6468f4d9-f386-43c6-a423-701d701bc1fa@glegroupsg2000goo.googlegroups.com>
I'm running ubuntu 11.04 using codeblocks.  I'm working on an SDL based game.  And, I keep getting the following error:  error: no match for ‘operator=’ in ‘*(((Animation*)this)->Animation::FrameList + ((unsigned int)(((unsigned int)index) * 44u))) = frame’

Here's a reduced representation of the code.

//character.h////////////////////////////////////////////////////////
class Frame
{
    public:
               Frame operator=(const Frame frame);/*<---...tried every
                                                   configuration of this line.*/
};

class Animation
{
  public:
           void SetFrame(int index, Frame * frame);
  private:
           Frame *FrameList;
}

//character.cpp //////////////////////////////////////////////////////////////

Frame Frame::operator=(const Frame frame)
{

}


void Animation::SetFrame(int index, Frame * frame)
{
 FrameList[index]=frame;
}

[toc] | [next] | [standalone]


#9146

FromIan Collins <ian-news@hotmail.com>
Date2011-08-22 11:42 +1200
Message-ID<9bdjf7Fr0aU4@mid.individual.net>
In reply to#9142
On 08/22/11 10:26 AM, Marz wrote:

[Please try a wrap your lines.  Looks like yet another google blunder]

> I'm running ubuntu 11.04 using codeblocks.  I'm working on an SDL based game.  And, I keep getting the following error:

error: no match for ‘operator=’ in 
‘*(((Animation*)this)->Animation::FrameList + ((unsigned int)(((unsigned 
int)index) * 44u))) = frame’

Why would you write such awful code?

> Here's a reduced representation of the code.
>
> //character.h////////////////////////////////////////////////////////
> class Frame
> {
>      public:
>                 Frame operator=(const Frame frame);/*<---...tried every
>                                                     configuration of this line.*/

Have you tried

Frame operator=( const Frame& );

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#9155

FromMarz <marzkrishna@gmail.com>
Date2011-08-21 20:17 -0700
Message-ID<bf22dc5c-c7da-46f5-b6a5-54f8a9461c2c@glegroupsg2000goo.googlegroups.com>
In reply to#9146
There's nothing that looks like that error written in the code.  It's simply the compilers effort to represent an array of Frame class pointers within the Animation class.  I've been trying to use strictly classes instead of C style structs.  The suggestion you made for the operator function is exactly what the compiler suggests.  But, it still won't recognize the operator.  And, I have no idea where 44u comes in.  I never defined anything as 44u.

[toc] | [prev] | [next] | [standalone]


#9166

FromNick Keighley <nick_keighley_nospam@hotmail.com>
Date2011-08-22 04:12 -0700
Message-ID<a966101e-1a18-47b6-a6d3-e8c7f68b295e@d25g2000yqh.googlegroups.com>
In reply to#9155
leave some context in

On Aug 22, 4:17 am, Marz <marzkris...@gmail.com> wrote:
> There's nothing that looks like that error written in the code.

what error? Yes i read the previous two posts and I'm still not clear
what you meant.

>  It's simply the compilers effort

What is?
The /compiler's/ effort?! Not your effort?


> to represent an array of Frame class pointers within the Animation class.

wouldn't that be?

class Animation
{
    Frame* arrayFramePtrs[9];
};


>  I've been trying to use strictly classes instead of C style structs.  The suggestion you made for the operator function is exactly what the compiler suggests. But, it still won't recognize the operator.

try a simpler case. Without all the casts and arbitary offsts

>  And, I have no idea where 44u comes in.  I never defined anything as 44u.

Try reposting again. I know posting software can do Strange Things.
But stuffing a 44u in there sounds way off the scale!



[toc] | [prev] | [next] | [standalone]


#9180

FromMarz <marzkrishna@gmail.com>
Date2011-08-22 10:15 -0700
Message-ID<4a5e99ea-d362-4c1e-8c33-51ca5ded9924@glegroupsg2000goo.googlegroups.com>
In reply to#9166
On Monday, August 22, 2011 6:12:43 AM UTC-5, Nick Keighley wrote:
> leave some context in
> 
> On Aug 22, 4:17 am, Marz <marzk...@gmail.com> wrote:
> > There's nothing that looks like that error written in the code.
> 
> what error? Yes i read the previous two posts and I'm still not clear
> what you meant.
> 
> >  It's simply the compilers effort
> 
> What is?
> The /compiler's/ effort?! Not your effort?
It's the compiler's output string.
> 
> 
> > to represent an array of Frame class pointers within the Animation class.
> 
> wouldn't that be?
> 
> class Animation
> {
>     Frame* arrayFramePtrs[9];
> };
I'm dynamically allocating memory at run time.
void Animation::SetNumFrames(int x)
{
 NumFrames=x;
 FrameList = (Frame *)  malloc(x * sizeof(Frame));
}
> 
> 
> >  I've been trying to use strictly classes instead of C style structs.  The suggestion you made for the operator function is exactly what the compiler suggests. But, it still won't recognize the operator.
> 
> try a simpler case. Without all the casts and arbitary offsts
> 
> >  And, I have no idea where 44u comes in.  I never defined anything as 44u.
> 
> Try reposting again. I know posting software can do Strange Things.
> But stuffing a 44u in there sounds way off the scale!

#ifndef CHARACTER_H
#define CHARACTER_H

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
//Defines/////////////////////////////////////////////////////////

//Definitions for various states//////////////////////////////////

#define STANDING 1
#define WALKING 2
#define AIRBORN 3
#define HITHI 4
#define HITLOW 5
#define TRIPPED 6
#define FALLING 7
#define GROUNDED 8
#define DEAD 9


//Classes////////////////////////////////////////////////////////
class ContactBox
{
    public:
        int GetStrenth();
        void SetStrength(int x);

        SDL_Rect GetArea();
        void SetArea(SDL_Rect x);

    private:
        int Strength;//how much damage can it withstand.
        SDL_Rect Area;//Area on the character.  EG. head.


};

class AttackBox //For when the character attacks
{
    public:

        void SetVelocity(int x, int y, int z);

        SDL_Rect GetArea();
        void SetArea(SDL_Rect x);

    private:
        int XVelocity;//The direction of the attack
        int YVelocity;//The direction of the attack
        int ZVelocity;

        SDL_Rect Area;//Area on the character.  EG. Fist.


};



class Frame
{
    public:

        void SetImage(char * filename, SDL_Rect area);//area of the image to use for the frame.

        void SetDuration(int Duration);
        int  GetDuration();

        void SetVelocity();

        void SetNextFrame(Frame * frame);
        Frame* GetNextFrame();

        void SetPreviousFrame(Frame * frame);
        Frame* GetPreviousFrame();

        void DrawFrame(SDL_Surface * screen);

        Frame &operator=( const Frame& );

    private:
        int FrameDuration;
        int XVelocity, YVelocity, ZVelocity;//The directional velocity of the frame.  To be added to the characters velocity.

        ContactBox * ContactBoxList;
        AttackBox * AttackBoxList;

        SDL_Surface* Image;//Set to null if character is not to be visible.
        SDL_Rect ImageArea;//to be passed to the drawing routine.

        Frame * PreviousFrame;
        Frame * NextFrame;
};

class Animation
{
  public:


        void UpdateAnimation(SDL_Surface* screen);
        void DrawAnimation(SDL_Surface* screen);

        void SetNumFrames(int x);
        void SetFrame(int index, Frame * frame);

        void SetNextAnimation(Animation * animation);
        Animation * GetNextAnimation();


  private:
        int NumFrames;
        int CurrentFrame;
        int ControlLock;//Should the user have control

        Frame *FrameList;//List of frames for the animation
        Animation * NextAnimation;//null if not linked.
};


class Character
{
    public:
        Character(char *filename);

        int GetHealth();
        void SetAnimation();




    private:
        int Health;//current character health
        int HealthMax;//max character health (can you say "Level Up!"
        int Level;
        int Experience;

        int Strenght;
        int Agility;
        int Intelligence;

        int State;
        int XLocation, YLocation, ZLocation;// where the character is located on the game map/stage
        int XVelocity, YVelocity, Zvelocity;//what direction the character is moving
        int XIntent, YIntent, ZIntent;//What direction the character is trying to go.

        int CurrentAnimation;
        int Visible; //should the character be visible

        Animation * AnimationList;
};





#endif

//CPP FILE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

#include <stdio.h>
#include <stdlib.h>
#include "character.h"



//ContactBox//////////////////////////////////////////////////////////

int ContactBox::GetStrenth()
{
 return Strength;
}

void ContactBox::SetStrength(int x)
{
 Strength = x;
}

SDL_Rect ContactBox::GetArea()
{
 return Area;
}
void ContactBox::SetArea(SDL_Rect x)
{
 Area = x;
}

//AttackBox///////////////////////////////////////////////////

void AttackBox::SetVelocity(int x, int y, int z)
{
    XVelocity = x;
    YVelocity = y;
    ZVelocity = z;
}

SDL_Rect AttackBox::GetArea()
{
  return Area;
}

void AttackBox::SetArea(SDL_Rect x)
{
    Area = x;
}

//Frame////////////////////////////////////////////////////////////////

void Frame::SetImage(char * filename, SDL_Rect area)//area of the image to use for the frame.
{
    Image = IMG_Load(filename);
    ImageArea = area;

}

void Frame::SetDuration(int Duration)
{

}

int  Frame::GetDuration()
{
 return FrameDuration;
}

void Frame::SetNextFrame(Frame * frame)
{
 NextFrame = frame;
}

Frame* Frame::GetNextFrame()
{
  return  NextFrame;
}

void Frame::SetPreviousFrame(Frame * frame)
{
 PreviousFrame = frame;
}

Frame* Frame::GetPreviousFrame()
{
 return  PreviousFrame;
}

void Frame::DrawFrame(SDL_Surface * screen)
{
SDL_BlitSurface(Image, 0, screen, &ImageArea);

}

//operator overloading
Frame &Frame::operator=( const Frame& )
{
    return *this;
}




//Animation///////////////////////////////////////////////////

void Animation::UpdateAnimation(SDL_Surface* screen)
{
    CurrentFrame++;

    DrawAnimation(screen);
}

void Animation::DrawAnimation(SDL_Surface* screen)
{
    FrameList[CurrentFrame].DrawFrame(screen);
}

void Animation::SetNumFrames(int x)
{
 NumFrames=x;
 FrameList = (Frame *)  malloc(x * sizeof(Frame));
}

void Animation::SetFrame(int index, Frame * frame)
{
 FrameList[index]=frame;
}

void Animation::SetNextAnimation(Animation * animation)
{
 NextAnimation = animation;
}

Animation * Animation::GetNextAnimation()
{
 return NextAnimation;
}


//Character///////////////////////////////////////////////////

Character::Character(char *filename)
{
    FILE *DefinitionFile;
    fopen(filename, "r");
}

int Character::GetHealth()
{
    return Health;
}

[toc] | [prev] | [next] | [standalone]


#9188

FromNobody <nobody@nowhere.com>
Date2011-08-22 20:00 +0100
Message-ID<pan.2011.08.22.19.00.56.16000@nowhere.com>
In reply to#9180
On Mon, 22 Aug 2011 10:15:20 -0700, Marz wrote:

> class Animation
> {

>         Frame *FrameList;//List of frames for the animation Animation *

> void Animation::SetFrame(int index, Frame * frame) {
>    FrameList[index]=frame;
> }

You're trying to assign a Frame* to a Frame. In C, this would be a
straightforward error. In C++, this is allowed, provided that you provide
a suitable operator= to perform the assignment. But you don't, hence the
error.

The above should be:

	FrameList[index] = *frame;

But it would be better to change the interface to take a Frame& rather
than a Frame*, i.e.:

	void Animation::SetFrame(int index, const Frame& frame) {
	   FrameList[index] = frame;
	}

[This is assuming that FrameList is actually supposed to be an array of
Frames, not an array of pointers to Frames.]

In general, you shouldn't use pointers unless you actually need the
functionality of a pointer (e.g. the ability to change where it points, or
to perform pointer arithmetic). If a reference will suffice, use a
reference.

Also, consider using std::vector instead of pointers and malloc().

[toc] | [prev] | [next] | [standalone]


#9207

FromMarz <marzkrishna@gmail.com>
Date2011-08-22 14:28 -0700
Message-ID<c09507e9-d85b-47cb-b6f1-1c0f93af1293@glegroupsg2000goo.googlegroups.com>
In reply to#9188
On Monday, August 22, 2011 2:00:56 PM UTC-5, Nobody wrote:
> On Mon, 22 Aug 2011 10:15:20 -0700, Marz wrote:
> 
> > class Animation
> > {
> 
> >         Frame *FrameList;//List of frames for the animation Animation *
> 
> > void Animation::SetFrame(int index, Frame * frame) {
> >    FrameList[index]=frame;
> > }
> 
> You're trying to assign a Frame* to a Frame. In C, this would be a
> straightforward error. In C++, this is allowed, provided that you provide
> a suitable operator= to perform the assignment. But you don't, hence the
> error.
> 
> The above should be:
> 
> 	FrameList[index] = *frame;
> 
> But it would be better to change the interface to take a Frame& rather
> than a Frame*, i.e.:
> 
> 	void Animation::SetFrame(int index, const Frame& frame) {
> 	   FrameList[index] = frame;
> 	}

This compiled correctly.  I didn't realize that the error wasn't caused by the compiler not finding the overloaded operator.
> 
> [This is assuming that FrameList is actually supposed to be an array of
> Frames, not an array of pointers to Frames.]
Yes,  that is correct.
> 
> In general, you shouldn't use pointers unless you actually need the
> functionality of a pointer (e.g. the ability to change where it points, or
> to perform pointer arithmetic). If a reference will suffice, use a
> reference.
> 
> Also, consider using std::vector instead of pointers and malloc().
I'm transitioning from C to C++ and figured I do it the old fashioned way before doing thorough research on vector syntax and caveats.  Just so I can get the project running.

Thanx for the help guys :)

[toc] | [prev] | [next] | [standalone]


#9168

From"Balog Pal" <pasa@lib.hu>
Date2011-08-22 13:25 +0200
Message-ID<j2tead$1j9c$1@news.ett.com.ua>
In reply to#9146
> Have you tried
>
> Frame operator=( const Frame& );

or rather the suggested canonical form

Frame &operator=( const Frame& )
{
 /// ...
 return *this;
}
?

or the even more suggested form of NOT having dtor, copy-ctor, op=, rather 
using members and base classes so the compiler-created version works 
perfectly? 

[toc] | [prev] | [next] | [standalone]


#9181

FromMarz <marzkrishna@gmail.com>
Date2011-08-22 10:19 -0700
Message-ID<5a8da22f-8c80-4a04-83c5-f5ecc0e315f7@glegroupsg2000goo.googlegroups.com>
In reply to#9168
On Monday, August 22, 2011 6:25:01 AM UTC-5, Balog Pal wrote:
> > Have you tried
> >
> > Frame operator=( const Frame& );
Tried every form of definition i could find on the net.  Including this one
> 
> or rather the suggested canonical form
> 
> Frame &operator=( const Frame& )
> {
>  /// ...
>  return *this;
> }
Not working either.
> ?
> 
> or the even more suggested form of NOT having dtor, copy-ctor, op=, rather 
> using members and base classes so the compiler-created version works 
> perfectly?
yeah,  if you check the code above, you'll notice that i don't define any constructors or destructors.

[toc] | [prev] | [next] | [standalone]


#9203

From"Balog Pal" <pasa@lib.hu>
Date2011-08-22 22:42 +0200
Message-ID<j2uevk$2c2p$1@news.ett.com.ua>
In reply to#9181
"Marz" <marzkrishna@gmail.com>
> yeah,  if you check the code above, you'll notice that i don't define any 
> constructors or destructors.

Too bad. Look up "rule of three" why it is bad almost everytime. 

[toc] | [prev] | [next] | [standalone]


#9182

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2011-08-22 13:32 -0400
Message-ID<j2u3rs$4no$1@dont-email.me>
In reply to#9142
On 8/21/2011 6:26 PM, Marz wrote:
> I'm running ubuntu 11.04 using codeblocks. I'm working on an SDL
> based
game. And, I keep getting the following error: error: no match for
‘operator=’ in ‘*(((Animation*)this)->Animation::FrameList + ((unsigned
int)(((unsigned int)index) * 44u))) = frame’
>
> Here's a reduced representation of the code.
>
> //character.h////////////////////////////////////////////////////////
> class Frame
> {
>      public:
>                 Frame operator=(const Frame frame);/*<---...tried every
>                                                     configuration of this line.*/

The idiomatic form is

      Frame& operator=(const Frame&);

A much less idiomatic form is

      Frame& operator=(Frame&);

There is a move assignment operator, but it's very new and you probably 
don't care about it.

> };
>
> class Animation
> {
>    public:
>             void SetFrame(int index, Frame * frame);
>    private:
>             Frame *FrameList;

It's an array of Frame objects.

> }
>
> //character.cpp //////////////////////////////////////////////////////////////
>
> Frame Frame::operator=(const Frame frame)
> {
>
> }
>
>
> void Animation::{

You probably mean

    void Animation::SetFame(int index, Frame * frame) {

>   FrameList[index]=frame;

Now, think about it.  'frame' is a *pointer* to Frame.  You're trying to 
assign a POINTER to an object.  Ain't gonna work, unless you define the 
assignment operator to take a pointer instead of a reference or a value.

> }

V
-- 
I do not respond to top-posted replies, please don't ask

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web