Thread: operator overloading... I'm stuck

  1. #16
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    That works if I have

    ooint a = "1";
    a = a + 1;

    but I can't have

    ooint a = "1";
    ooint b = "2";
    a = a + b;

    Because of this:

    error C2679: binary '+' : no operator defined which takes a right-hand operand of type 'class ooint' (or there is no acceptable conversion)

    Wouldn't that mean you have to have another possibility for the operator overload?

  2. #17
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    i think i may be able to clear it up a little for you. If you want to do something like this:

    ooint = int + int;

    that's fine, but i think you are looking at it the wrong way. the
    int + int part is the integer addition operator, which has already been defined and it returns an integer. So, this:

    ooint = int + int;

    really means this:

    ooint = int;

    because int + int returns an int. So, the overloaded = operator for the ooint class really only needs to take one argument in this case, which is an int. If you want to do this:

    ooint a = "1";
    ooint b = "2";
    a = a + b;

    what you are really doing there is adding two ooints. Have you overloaded the + operator for ooint? If so, do you have one that takes another ooint as an argument? If you don't, you can just add it:

    operator +(ooint operand)
    {
    //whatever you wanna do
    }

    Hope that helps a little

  3. #18
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I'm horrible at operator overloads, I know I have to do that but I don't know how.

  4. #19
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    That works if I have

    ooint a = "1";
    a = a + 1;

    but I can't have

    ooint a = "1";
    ooint b = "2";
    a = a + b;

    Because of this:

    error C2679: binary '+' : no operator defined which takes a right-hand operand of type 'class ooint' (or there is no acceptable conversion)

    Wouldn't that mean you have to have another possibility for the operator overload?

    Look dude, you have to overload an operator for every type you want to use it with, if you want to do ooint b = a + 1; then you've got to define
    Code:
    ooint operator + ( const ooint & lhs, int rhs )
    If you want to do ooint c = a + b; then you've got to define
    Code:
    ooint operator + ( const ooint & lhs, const ooint & rhs )
    And so on for every type you plan on using for the + operator. Unless C++ has a one step conversion from the type you want to use to the type that + is defined for, you've gotta overload again or it won't work.

  5. #20
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Perfect! Thank you! That's exactly what I needed!

  6. #21
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Originally posted by PorkyChop


    ooint = int + int;

    that's fine, but i think you are looking at it the wrong way. the
    int + int part is the integer addition operator, which has already been defined and it returns an integer. So, this:

    ooint = int + int;

    really means this:

    ooint = int;
    How do I handle for that?

  7. #22
    Registered User
    Join Date
    Jul 2002
    Posts
    66

    quote:
    --------------------------------------------------------------------------------
    Originally posted by PorkyChop


    ooint = int + int;

    that's fine, but i think you are looking at it the wrong way. the
    int + int part is the integer addition operator, which has already been defined and it returns an integer. So, this:

    ooint = int + int;

    really means this:

    ooint = int;
    --------------------------------------------------------------------------------



    How do I handle for that?
    You don't, int + int is built into the compiler, so it does everything for you as long as ooint has an overloaded = operator that takes an int.

  8. #23
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    a = 1+2;
    cout << a << endl << endl;

    That outputs a heart to the screen.

    [EDIT]
    Oh.... I'd have to overload the =, wouldn't I?

  9. #24
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    a = 1+2;
    cout << a << endl << endl;

    That outputs a heart to the screen.
    I'm surprised that even compiled, assuming that a is an ooint because the ostream operator << isn't supposed to take ooints as operands. You need a method in your ooint class, like value(), that returns the value of the integer attribute(or char array as an integer), so it would be something like this:

    a = 1 + 2;
    cout <<a.value() <<endl <<endl;

  10. #25
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    the operator << is overloaded, so cout works just fine.

    Its the = that is the problem, I think.

  11. #26
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    the operator << is overloaded, so cout works just fine.

    Its the = that is the problem, I think.
    do you have an overloaded = like this?
    ooint operator=( int rhs )
    If you do then it's all good

  12. #27
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    the operator << is overloaded, so cout works just fine.

    Its the = that is the problem, I think.
    huh? how did you manage to overload the << operator for ostream? you can overload it for ooint, but i don't know how you would overload it for ostream...maybe i just don't know what i'm talkin about, but i doubt it. it doesn't matter how you have implemented the = operator, as long as it assigns the int value given to it to the char array correctly.

  13. #28
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    huh? how did you manage to overload the << operator for ostream? you can overload it for ooint, but i don't know how you would overload it for ostream...maybe i just don't know what i'm talkin about, but i doubt it. it doesn't matter how you have implemented the = operator, as long as it assigns the int value given to it to the char array correctly.
    Right, you overload << for ooint to act on an ostream
    Code:
    ostream& operator<<( ostream& os, const ooint& stuff )
    {
        //print stuff
    }
    then you can do stuff like cout << a << endl because cout is an ostream object and a is an ooint object.

  14. #29
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    ok...sorry bout that. Guess I learned somethin too

  15. #30
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Talking

    Originally posted by Crimpy
    do you have an overloaded = like this?
    ooint operator=( int rhs )
    If you do then it's all good
    I'm not quite sure how to do this, but it makes sense. Lol, I guess I'm pathetic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM