Jump to content
  • Advertisement
Sign in to follow this  
Scet

Operator overloading weirdness [solved]

This topic is 4526 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having some trouble getting one of my structs to compile thanks to my compiler completely ignoring my operator statement. Anyway here's the code:
struct {

	unsigned char *Pntr;
	void operator=(unsigned char *Address){Pntr=Address;}
	unsigned char Data[8][8];
	bool Built;

	//some functions

} Tile[0x10]={
	Pointer(0x8000),
	Pointer(0x8010),
	Pointer(0x8020),
	etc...


Pointer returns an unsigned char pointer. Now this works for some other classes I made that only have the Pntr variable and this struct will compile if I comment out Data, Build and the lines using them. GCC gives my this error: invalid conversion from `unsigned char*' to `unsigned char' Thinking that GCC is retarded and trying to second guess me since Pntr and Data are both pointers, I tried switching Data to multiple types. I've tried short, long, float, but they all give me something like the error above. Anyone have any ideas on how to fix this? Note: Due to how my project is structured I have to define them like this, also Data and Pntr do not point to the same block of memory. [Edited by - Scet on January 4, 2006 10:14:34 AM]

Share this post


Link to post
Share on other sites
Advertisement

unsigned char* Pointer(Pair Address){
return &MemoryMap[Address.High][Address.Low];}


returns an unsigned char pointer from a certain block of memory(it's for my emulator). Plenty of other things use Pointer and they work just fine so I doubt it's the problem.

Share this post


Link to post
Share on other sites
Ok, then I see the problem. You're using an aggregate initializer list to initialize your struct array. You're expecting that the first member of the initializer list will be used to initialize the first struct and the second member of the initializer list will be used to initialize the second struct. That's not what happens. Because your struct has no constructor, the compiler tries to use aggregate initializers to intialize the struct. This means that the first Pointer() call is used to initialize the Pntr member, the second Pointer() call is used to intialize the Data[0][0], the third Pointer() call is used to initialize Data[0][1], and so on. Ah the joy of C compatibility.

I think the only way to get things to initialize like you seem to want is to name the struct and give it a constructor.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
I think the only way to get things to initialize like you seem to want is to name the struct and give it a constructor.

Or add additional braces to segregate the individual struct instances:
Tile[0x10]={
{Pointer(0x8000)},
{Pointer(0x8010)},
{Pointer(0x8020)},
// etc...

This will only work as long as Pntr is the first member of the struct though.

EDIT: Just to be clear, the compiler is ignoring your operator= because this is initialisation, not assignment. operator= is invoked when an already existing object is given a new value. Your Tile array is creating new objects instead.

Enigma

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!