Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / My Point struct

Author
Message
tomtetlaw
15
Years of Service
User Offline
Joined: 18th Jan 2009
Location:
Posted: 8th Aug 2009 11:28
When I run this code:


I get this error, even though I've defined the operators:


Here is where I have defined them:


I don't know why it's telling me that I havn't defined them..

Any help would be greatly appreciated

#ifdef __NEWBIE__
MakeAnAwesomeGame(lots of badies, lots of guns, lots of stuff to do, BIG level)
#endif
Mista Wilson
16
Years of Service
User Offline
Joined: 27th Aug 2008
Location: Brisbane, Australia
Posted: 8th Aug 2009 12:06
To overload a class or structure's operators you need to overload them as part of the class or struct dont you ? ie. as member functions essentially .... the way you have them done at the moment, I assume your compiler doesnt recognize they are supposed to be a part of the struct or class as they arent in it.

Here is a class I use now and then for integer points, like mouse position, it has overloaded operators :

INTPOINT.h :



If it ain't broke.... DONT FIX IT !!!
tomtetlaw
15
Years of Service
User Offline
Joined: 18th Jan 2009
Location:
Posted: 8th Aug 2009 12:24
Overloading the global operators should work also, it was in a book by Bjarne Strousrup.

#ifdef __NEWBIE__
MakeAnAwesomeGame(lots of badies, lots of guns, lots of stuff to do, BIG level)
#endif
tomtetlaw
15
Years of Service
User Offline
Joined: 18th Jan 2009
Location:
Posted: 8th Aug 2009 12:26
The I have used the other operators that I have defined there without error, but those particular ones wont work for some reason

#ifdef __NEWBIE__
MakeAnAwesomeGame(lots of badies, lots of guns, lots of stuff to do, BIG level)
#endif
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 8th Aug 2009 18:06 Edited at: 8th Aug 2009 18:31
I might point out that returning a void on many of your operations is probably a bad idea also. Any assignment operation in turn represents the resulting value assigned. Using ints for the moment,

int a = 10;
int b = 5;
int c;

c = (a += b);

The value of a becomes 15 but the operation itself also carries the value of 15, with or without the parenz, so should return the resulting value of the new value of a.

Point2df operator+= (Point2df a, Point2df b) {a.x+=b.x; a.y+=b.y; return a;}

The ones to watch out for are the post increment/decrement. They return the original value of the variable and the increment/decrement is carried out after the returned value.

Point2df operator++ (Point2df p) {Point2df t = p; p.x++; p.y++; return t;}

Your problem may actually be in the returned type, though that's not really apparent from the error messages your getting. It does say

Quote: "Point2df' does not define this operator or a conversion to a type acceptable to the predefined operator"


So the issue may be in the "conversion to a type acceptable" part.

EDIT: I just noticed these structure types templated? If so, you may have to define these operators within the struct. I'm not sure about that though.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
tomtetlaw
15
Years of Service
User Offline
Joined: 18th Jan 2009
Location:
Posted: 9th Aug 2009 11:07
Point2df operator++ (Point2df p) {Point2df t = p; p.x++; p.y++; return t;}

shouldnt you increment t's values?

#ifdef __NEWBIE__
MakeAnAwesomeGame(lots of badies, lots of guns, lots of stuff to do, BIG level)
#endif
tomtetlaw
15
Years of Service
User Offline
Joined: 18th Jan 2009
Location:
Posted: 9th Aug 2009 11:47
Ok, i've changed the way i do my operators, now this is my operators



This is where I use them:


And for some reason it gives me this error:


??

#ifdef __NEWBIE__
MakeAnAwesomeGame(lots of badies, lots of guns, lots of stuff to do, BIG level)
#endif
Mista Wilson
16
Years of Service
User Offline
Joined: 27th Aug 2008
Location: Brisbane, Australia
Posted: 9th Aug 2009 12:14
That error means that its expecting those operators to be members of the relevant struct or class, is there a reason that you have to declare them as global ?

I mean, if you declare them as member functions of the class, they will still work in the way you are using them above..

I think the logic you are after would be somthing like :

declare += and -= overloads in the global scope, which will operate on Point2df or whatever, calling that class's own += nad -= overloaded operators(defined inside that class) to perform the actual operation...

If it ain't broke.... DONT FIX IT !!!
tomtetlaw
15
Years of Service
User Offline
Joined: 18th Jan 2009
Location:
Posted: 9th Aug 2009 13:07
Ok, I fixed it, I just put them into the class definitions

Thanks for your help.

Sorry for the triple post!

#ifdef __NEWBIE__
MakeAnAwesomeGame(lots of badies, lots of guns, lots of stuff to do, BIG level)
#endif
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 9th Aug 2009 13:12
There's no reason for you to use global operators there as none of those operators require they be global so I guess it's neater to put them in the class(keeps spam out of the global scope). Also, those are highly inefficient, all of those operators copy the data types in, you should use const references instead where you can.

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 9th Aug 2009 20:12
Quote: "Point2df operator++ (Point2df p) {Point2df t = p; p.x++; p.y++; return t;}

shouldnt you increment t's values?"


Nope. Remember what I said about post increment/decrement. The value of the operation is the original value of the variable. The increment/decrement is a side effect that occurs after value is appraised.

int a = 45;
int b = a++;

The value assigned to b would still be 45.

int a = 45;
int b = ++a;

The value assigned to b would be 46.

One other side note to keep in mind is that if you use increment/decrement operators on the same variable within the same expression there is no guarantee that the operation will take place as the variable is read in the expression, only that it will occur by the end of the statement. At least this was true by the standards when I was teaching the language. There may have been some changes in the meantime. You'd still have to be aware of the standard used by your particular compiler's implementation.

int a = 5;
int b = a++ + a;

Normally you'd expect this to read a as being 5 the first time, then incremented to 6 so that its second use would be 6 and that you'd expect the value of the expression to be 11. However, there is no guarantee that the value of a gets incremented when it's value is used. However, following the semi-colon the a would definitely be incremented.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Amnzero
15
Years of Service
User Offline
Joined: 1st Aug 2009
Location:
Posted: 9th Aug 2009 20:32 Edited at: 9th Aug 2009 20:34
From what I understand about the ++ operator the original number is returned before it gets incremented if it appears on the right side, and after if it is on the left.

a = 1
++a + a; and a++ + a;

should return 4 and 3 respectively. Of course you may be right about the changes Lilith. The only thing I have used the ++ operators for in a long time are for loops. Other than that I am prety much in love with += 1 for readability.

if(enemy == Amnzero) runAway();
Amnzero->WebSite = L"http://neovance.com/";
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 9th Aug 2009 20:42
Quote: "should return 3 and 2 respectively."


Not necessarily. Remember I said that the incrementation of the variable a isn't guaranteed to happen until it reaches the end of the statement. The other side of that statement is that it could be incremented at the time it's first used in the expression and thus the value of amight be 2 when it's used in the expression. So you could have 4 either way also. It depends on the compiler's implementation.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 9th Aug 2009 20:48
Quote: "At least this was true by the standards when I was teaching the language."

Absolutely, and it's still true for VC++ and GCC (the two compilers I tend to use).

++a + a; ... is undefined.
a++ + a; ... is undefined.

The compiler is under no obligation to execute the statements in left-to-right order - it may be more efficient to carry out the calculations out of order, or to execute the increment immediately - it may be more efficient to delay the incrementing until the end of the statement.

A particular compiler may tend to give you more predictable results, but even then, the optimiser can trip you up sometimes when it re-orders the statement.

Basically, don't do it. It MAY result in a few more keys being pressed by you, but that's a lot better than code that gives different results depending on how it's compiled or linked.

Amnzero
15
Years of Service
User Offline
Joined: 1st Aug 2009
Location:
Posted: 9th Aug 2009 22:22 Edited at: 9th Aug 2009 22:23
Of course you could allways use grouping b = (a++) + a; I think in VC++ the () would make sure that a was incremented before completing the equasion.

if(enemy == Amnzero) runAway();
Amnzero->WebSite = L"http://neovance.com/";
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 9th Aug 2009 23:05 Edited at: 9th Aug 2009 23:18
I wouldn't bet on that. Parenz force order of evaluation but does nothing to enforce side effects. The only thing that I know, besides the end of statement, that forces all side effects to take place is the comma operator. It basically defines a critical point and specifies that all side effects are updated at that point. However, it's difficult to implement it in a mathematical expression. It can a bit confusing too in that

int a = 10;
int b = 4;
int c;

c = ++a, b;

Normally you'd think that c would be assigned the incremented value of a. But it wouldn't. By the end of the statement a would be incremented but c would be assigned the value of b. So the side effect on a would be implemented but for the purposes of the assignment to c that value is thrown out and evaluation passes to the next expression, which would be b.

To do what you intended to do

b = (a++) + a;

you'd have to do something like this

b = a++, b += a;

in order to use the comma operator. The question is, is it worth it in terms of execution time or clarity of intent. Of course, the same is true about other workarounds.

EDIT: Just some additional information on use of the comma operator.

Sometimes the comma operator is used to do multiple operations in a single expression. Note the following example of a for loop.


int i;
int j;
int result = 0;

for (i = 0, j = 10; i <= 10; i++, j--)
{
result += i * j;
}

You can pre-assign both i and j within the initializing part of the for statement and increment i while decrementing j without having to put one or the other in the body of the loop. There are also some fancy things you can do within the conditional segment of the for statement such as changing the above to

for (i = 0, j = 10; ++i, --j; )

The test for the condition would look at the i++, implement the side effect but, because of the comma operator, ignore the true/falseness of the evaluation. It would go on to look at the next expression, decrement the value of j and since there's no comma after that expression use the true falseness of its value to determine if the loop should continue. Once j falls to zero the evaluated expression is false and the loop terminates.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
tomtetlaw
15
Years of Service
User Offline
Joined: 18th Jan 2009
Location:
Posted: 10th Aug 2009 00:35
Thanks for all your replies.

#ifdef __NEWBIE__
MakeAnAwesomeGame(lots of badies, lots of guns, lots of stuff to do, BIG level)
#endif
Amnzero
15
Years of Service
User Offline
Joined: 1st Aug 2009
Location:
Posted: 10th Aug 2009 00:45
Good to know. Hard to follow. Makes your code a little hard to read sometimes. I thought I read somewhere in the VC++ documentation that side effects where applied inside ()... Probably wrong though.

Since C does not define the order of evaluation of side effects, both evaluation methods discussed above are correct and either may be implemented. To make sure that your code is portable and clear, avoid statements that depend on a particular order of evaluation for side effects.

if(enemy == Amnzero) runAway();
Amnzero->WebSite = L"http://neovance.com/";

Login to post a reply

Server time is: 2024-10-01 10:33:42
Your offset time is: 2024-10-01 10:33:42