operator overloading... I'm stuck

This is a discussion on operator overloading... I'm stuck within the C++ Programming forums, part of the General Programming Boards category; I'm working on a class I call a "int class" that allows weird manipulation of an "integer" which is stored ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    operator overloading... I'm stuck

    I'm working on a class I call a "int class" that allows weird manipulation of an "integer" which is stored as a dynamically allocated character array. I want to overload all operators that are usable with an integer, so that

    ooint i = 0;

    works just as well as

    ooint i = '0';

    or

    ooint i = "0";

    In fact, I don't want it to take it as a character or string at all! I want to be able to do

    i = i+6;

    so that in the class, it has something like itoa((atoi(i))+6),dummybuffer,10); to make it act like an integer, only stored as a string so that my weird manipulations can work.

    I tried overloading the + like this:

    Code:
    ooint operator + (const int & lhs, const int & rhs)
    {
        char dummybuffer[1024];
    	int iResult = lhs + rhs;
    	itoa(iResult,dummybuffer,10);
    	ooint result(dummybuffer); // copies lhs to result
        return result;	  // returns a copy of result
    }
    And I get this:

    c:\filepath\ooint.cpp(187) : error C2803: 'operator +' must have at least one formal parameter of class type

    Any ideas as to how I could get this to work? I don't want to have to do this:

    i = "1" + "6"

    If I had const ooint, then it would have to take it as a string, wouldn't it?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >ooint operator + (const int & lhs, const int & rhs)
    You can't overload the built-in types, so this is an illegal function. The overloaded operator must have at least one argument that is the integer class you're writing.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I read the error, so that doesn't help. What I wanted to know is how to have the same effect as what I was trying.

    I want it to take input as an integer (atoi works, as long as it doesn't have to have single (') or double (") quotes around it), run the calculation, and convert it to a string (itoa), and return the string as an ooint class object.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What I wanted to know is how to have the same effect as what I was trying.
    Here's a quickie solution, apologies for the crudity:
    Code:
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <limits>
    
    class Integer
    {
      char base[std::numeric_limits<int>::digits + 1];
    public:
      Integer ( int i ) { sprintf ( base, "%d", i ); }
    
      operator int()
      {
        return atoi ( base );
      }
    
      Integer& operator= ( const Integer& i )
      {
        sprintf ( base, "%s", i.base );
    
        return *this;
      }
    
      Integer& operator+= ( int rhs )
      {
        int hold = atoi ( base ) + rhs;
    
        sprintf ( base, "%d", hold );
    
        return *this;
      }
    
      friend std::ostream operator<< ( std::ostream out, const Integer& i )
      {
        return out<< i.base;
      }
    };
    
    const Integer operator+ ( const Integer& lhs, int rhs )
    {
      return Integer ( lhs ) += rhs;
    }
    
    int main()
    {
      int a = 5;
      Integer i = 10;
    
      std::cout<< i <<std::endl;
    
      i += 5;
    
      std::cout<< i <<std::endl;
    
      i = i + 5;
    
      std::cout<< i <<std::endl;
    
      a += i;
    
      std::cout<< a <<std::endl;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Unhappy

    That looks good, I guess, but I have no idea what it does or how to implement it.

    I'll attatch the newest version of my class files if you would be so kind as to post an updated version. Otherwise, I'm clueless.

    Thanks!
    Attached Files Attached Files

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but I have no idea what it does or how to implement it.
    It's actually quite simple, the constructor converts an int to a string and assigns that string to base. The overloaded = operator does the same thing, the overloaded += operator converts base to an int with atoi and adds it's argument to that value, then uses sprintf to convert back to a string and assign to base. The overloaded + operator uses the overloaded += operator to do all of the dirty work.

    The only part that might be confusing is

    char base[std::numeric_limits<int>::digits + 1];

    which sets the size of base as the number of bits - 1 in a signed int + 1 for the nul character. Overkill, yes, but oh well.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I think I see what you mean, but that won't help me because I have a lot of member functions in my ooint class that probably wouldn't work anymore, and I don't know how to combine your class and my class so that it works right.

    Thanks again!

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Prelude, how do I use your code inside my class?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Prelude, how do I use your code inside my class?
    From what I can tell you should be able to just plug it in. All of your operations work with strings, so integration shouldn't be much trouble.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    That's a whole separate class, though, isn't it?

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >That's a whole separate class, though, isn't it?
    By plug it in I meant pinch the member functions from my class and place them in your class.

    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    *goes ahhh.... gets a clue*

    So I just rename "Integer" to "ooint" and I'm done?

  13. #13
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Prelude, can you explain exactly what you mean?

    Do I need

    char base[std::numeric_limits<int>::digits + 1];
    public:
    Integer ( int i ) { sprintf ( base, "%d", i ); }

    or just the overloads?

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Prelude, can you explain exactly what you mean?
    Since you're having so much trouble, just add this. It answers your original question:
    Code:
    #include <cstdio>
    
    ooint operator + ( const ooint & lhs, int rhs )
    {
        int hold = lhs.converttoint();
    
        hold += rhs;
    
        char s[10];
        sprintf ( s, "%d", hold );
    
        return ooint ( s );
    }
    -Prelude
    My best code is written with the delete key.

  15. #15
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    thanks, I'll try that in a minute.

Page 1 of 3 123 LastLast
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, 11:40 AM
  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21