Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Need basic programming help (C++) Round 2!  (Read 1141 times)

Shadowgandor

  • Bay Watcher
    • View Profile
Need basic programming help (C++) Round 2!
« on: June 19, 2012, 06:46:15 am »

Well hello there people of Bay12, I'm trying to overload an operator in C++ but I just can't seem to do it. Seeing how it's pretty basic stuff, I thought someone on here might be able to help me.
I'm trying to overload the operation so that if I use Point1>Point2, it returns weither Point1.x+Point1.y is larger then Point2.x+Point2.y. Pretty useless but atleast I'd be able to see how overloading works.

This is what I have now:
Spoiler (click to show/hide)

The important part is:

Spoiler (click to show/hide)

Now, the operator> function says ''Unable to overload function distinguished with return type alone'' but I have no idea what I can do. Anyone know what I'm doing wrong?


ps: I recall some other programming help request threads, maybe we can turn this one into a ''Ask all your programming questions here!'' thread if people are interested.
« Last Edit: June 24, 2012, 10:14:18 am by Shadowgandor »
Logged

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: Need basic programming help (C++)
« Reply #1 on: June 19, 2012, 07:15:35 am »

The problem here is that operator overloading is like function overloading in that you need to have different parameter sets for each overload. I would get rid of the version of operator> that returns a Point (unless you could provide a meaningful explanation of such an operator). Incidentally, my memory tells me that a class instance can access private members of other instances of the same class, so you could replace (x+y)>(c.getX()+c.getY()) with (x+y)>(c.x+c.y).
Logged

Shadowgandor

  • Bay Watcher
    • View Profile
Re: Need basic programming help (C++)
« Reply #2 on: June 19, 2012, 07:33:12 am »

*facepalm* Indeed! Just removing "Point operator>(Point& c);" was the solution! Thanks for the quick response! And yes, you can use c.x and c.y, so that too helped a lot :)
Logged

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: Need basic programming help (C++)
« Reply #3 on: June 19, 2012, 07:40:43 am »

Seeing how it's pretty basic stuff

I see what you did there!
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Shadowgandor

  • Bay Watcher
    • View Profile
Re: Need basic programming help (C++) Round 2!
« Reply #4 on: June 24, 2012, 10:18:23 am »

And I'm back for more! Right now I'm trying the following thing:

PriorityQueue PriorityQueue::operator+(const PriorityQueue& c) const{
         PriorityQueue <T> Test(size_+c.size_);
         for(int i=0;i<size_;i++){
            Test.push(list);
         }
         for(int i=0;i<c.size_;i++){
            Test.push(c.list);
         }
         return Test;
      }

Entire code in spoiler:
Spoiler (click to show/hide)

Now this doesn't seem to work as all Integers are -842150451 now when I pop them.
Anyone have an idea what's going on? Sorry for pestering you folks with all these questions btw ^^
Logged

Shadowgandor

  • Bay Watcher
    • View Profile
Re: Need basic programming help (C++) Round 2!
« Reply #5 on: June 24, 2012, 04:45:59 pm »

Nevermind. Found the solution. I used size_ instead of top_. Fail!
Logged