Operator overloading in memberclass.

M

Marz

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::eek:perator=(const Frame frame)
{

}


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

Ian Collins

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& );
 
M

Marz

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 withinthe 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..
 
N

Nick Keighley

leave some context in

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!
 
B

Balog Pal

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?
 
M

Marz

leave some context in



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


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));
}
try a simpler case. Without all the casts and arbitary offsts


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::eek:perator=( 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;
}
 
M

Marz

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.
 
V

Victor Bazarov

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::eek:perator=(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
 
N

Nobody

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().
 
B

Balog Pal

Marz said:
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.
 
M

Marz

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top