overload operator+ in format (z + obj2)

This is a discussion on overload operator+ in format (z + obj2) within the C++ Programming forums, part of the General Programming Boards category; I need help to overload the operator + in the format (z + obj2). I have done it in two ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    14

    overload operator+ in format (z + obj2)

    I need help to overload the operator + in the format (z + obj2).

    I have done it in two formats where 'n' becomes the sum of two objects and 'z' is z (scalar variable/constant/expression).
    1. n = obj2 + obj 1 (done)
    ---> declaration
    double operator+(const product&);
    ----> definition
    // Overloading the operator+ (productObj2 + productObj1)
    double product:perator +(const product & rhs)
    {
    return (*this).productCost + rhs.productCost ;
    }


    2. n = obj2 + z (scalar variable/constant/expression) (done)
    ---> declaration
    double operator+(const double&)----> definition
    ----> definition
    double product:perator +(const double & rhs)
    {
    cout << "The operator+ (productObj2 + z) function ran." << endl;
    return (*this).productCost + rhs ;
    }



    3. n = z + obj2 <---------- Need help/tips/comments to overload operator +

    <Need help/tips/comments to overload operator + in the above format>


    Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Make it a friend to the class, with the signature operator+(const double &, const obj &)
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    14

    Need to overload opertaor+

    Thanks zx-1, for you reply. But I am still having the issue.

    Declaration-->
    friend double operator+(const double&, const product &);

    Definition-->
    double product:: operator +(const double & num, const product & rhs )
    {
    ......
    }


    But I am still getting the following error:
    error C2511: '+' : overloaded member function 'double (const double &,const class product &)' not found in 'product'


    I am not sure why compiler is still complaining?

    Quote Originally Posted by zx-1
    Make it a friend to the class, with the signature operator+(const double &, const obj &)

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Because the friend is not a member. Remove the product:: from the definition.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    14

    It worked!! Thanks.

    It worked finally!! Thanks a lot for the quick reply.

    Quote Originally Posted by CornedBee
    Because the friend is not a member. Remove the product:: from the definition.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    14

    overload operator+=

    I am trying to overload operator+=, so that "obj += n" functionality can be accomplished. The code below doesn't put the new value added value of "obj + n" in obj data member.

    Can somebody please point the error in the following syntax and help me get the correct syntax to accomplish "obj+= n" functionality?

    Code:
    double productCost ; // a private member of the class
    
    Declaration:
    double operator+=(const double);
    
    Definition:
    double product::operator +=(const double pCost)
    {
        (*this).productCost = (*this).productCost + pCost ;
        return (*this).productCost;
    }
    Thanks in advance.

  7. #7
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    your code actually looks like it should work.
    typically with ops like +=, -= and so on, you generally return a reference to this to allow operator chaining
    Code:
    Declaration:
    class product
    {
    public:
        // constructors, etc
        product &operator+=(double cost); // const not necessary
    
    private:
        double productCost; 
    };
    
    Definition:
    product & product::operator +=(double cost)
    {
        this->productCost += cost;
        return *this;
    }
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  8. #8
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    The declaration (if you're making it a member function) should read:
    Code:
    double product::operator+=(double);
    Other than that, it should work fine. You don't have to use (*this).productCost to reference that member. You can access the private members by name, like this:
    Code:
    double product::operator+=(double pCost)
    {
      productCost += pCost;
      return pCost;
    }
    [edit]oops, beaten...I went off to test something and forgot to check if anyone had replied...
    Last edited by Decrypt; 10-09-2006 at 10:25 PM.
    There is a difference between tedious and difficult.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Quote Originally Posted by slrj
    I am trying to overload operator+=, so that "obj += n" functionality can be accomplished. The code below doesn't put the new value added value of "obj + n" in obj data member.

    Can somebody please point the error in the following syntax and help me get the correct syntax to accomplish "obj+= n" functionality?

    Code:
    double productCost ; // a private member of the class
    
    Declaration:
    double operator+=(const double);
    
    Definition:
    double product::operator +=(const double pCost)
    {
        (*this).productCost = (*this).productCost + pCost ;
        return (*this).productCost;
    }
    Thanks in advance.
    Why are you writing so much more than you need to?
    (*this). is redundant.
    And why not use operator += internaly?
    Code:
    double product::operator +=(double pCost)
    {
        productCost += pCost;
        return productCost;
    }
    Also, you would usually return a reference to the object being modified:
    Code:
    return *this;
    Last edited by iMalc; 10-10-2006 at 12:44 AM.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    14

    Thanks for the tips/help...

    ChaosEngine/Decrypt/Imalc,

    I really appreciate your help and tips. My code works now.

    Thanks.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    14

    Next hic-up in overloading the extraction (>>)operator

    The overloading of extraction operator(>>) is giving the following errors. The insertion (<<)operator is working within the code. It looks like there are some header files missing to be included? or there are other errors which I can't see.

    Code:
    Declaration:
     #include <iostream>
    .
    .
    class x{
    	friend ostream &operator<<(ostream &, const product &); <--- Work fine
    	friend istream &operator>>(istream &, product &); <---- Giving the errors shown below 
    
    public:
    }
    
    
    Definition:
    istream &operator >>(ostream &input, product &p)
    {
    .........
    }
    
    Compiler Error: 
    ------------------Configuration: useProduct - Win32 Debug--------------------
    Compiling...
    product.cpp
    error C2143: syntax error : missing ';' before '&'
    error C2433: 'istream' : 'friend' not permitted on data declarations
    error C2501: 'istream' : missing storage-class or type specifiers
    error C2244: 'istream' : unable to resolve function overload
    error C2061: syntax error : identifier 'istream'
     error C2501: '>>' : missing storage-class or type specifiers
    error C2805: binary 'operator >>' has too few parameters
    Error executing cl.exe.
    
    product.obj - 7 error(s), 0 warning(s)
    Thanks.

  12. #12
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Either add
    Code:
    using namespace std;
    immediately after your #include line, or prefix istream/ostream etc with std::

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    14

    Declared namespace uasge

    Skeane,

    I do have "using namespace std:" declared in the .cpp class definition, but the problem still persists. Do you have any other suggestions?


    Quote Originally Posted by SKeane
    Either add
    Code:
    using namespace std;
    immediately after your #include line, or prefix istream/ostream etc with std::

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    14

    typo caught......

    Typo caught in definition of the overloaded function (&operator>>). The parameter "input" should have been of type "istream" instead of "ostream".

    Incorrect Definition:
    istream &operator >>(ostream &input, product &p)
    {
    .........
    }


    Correct Definition:
    istream &operator >>(istream &input, product &p)
    {
    .........
    }



    Thanks.

    Quote Originally Posted by slrj
    The overloading of extraction operator(>>) is giving the following errors. The insertion (<<)operator is working within the code. It looks like there are some header files missing to be included? or there are other errors which I can't see.

    Code:
    Declaration:
     #include <iostream>
    .
    .
    class x{
    	friend ostream &operator<<(ostream &, const product &); <--- Work fine
    	friend istream &operator>>(istream &, product &); <---- Giving the errors shown below 
    
    public:
    }
    
    
    Definition:
    istream &operator >>(ostream &input, product &p)
    {
    .........
    }
    
    Compiler Error: 
    ------------------Configuration: useProduct - Win32 Debug--------------------
    Compiling...
    product.cpp
    error C2143: syntax error : missing ';' before '&'
    error C2433: 'istream' : 'friend' not permitted on data declarations
    error C2501: 'istream' : missing storage-class or type specifiers
    error C2244: 'istream' : unable to resolve function overload
    error C2061: syntax error : identifier 'istream'
     error C2501: '>>' : missing storage-class or type specifiers
    error C2805: binary 'operator >>' has too few parameters
    Error executing cl.exe.
    
    product.obj - 7 error(s), 0 warning(s)
    Thanks.

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    14

    Code comiples on Unix (Solaris) but not on Visual C++

    Code comiples on Unix (Solaris) but not on Visual C++? Any suggestions....

    --------------------Configuration: useProduct - Win32 Debug--------------------
    Compiling...
    product.cpp
    c:\scc\assign 2\product.h(39) : fatal error C1001: INTERNAL COMPILER ERROR
    (compiler file 'msc1.cpp', line 1786)
    Please choose the Technical Support command on the Visual C++
    Help menu, or open the Technical Support help file for more information
    Error executing cl.exe.

    product.obj - 1 error(s), 0 warning(s)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  2. function returning hour in either 12 or 24 hour format
    By stanlvw in forum C Programming
    Replies: 4
    Last Post: 01-01-2008, 05:02 AM
  3. Compression/Decompression Wave File and MP3
    By cindy_16051988 in forum Projects and Job Recruitment
    Replies: 51
    Last Post: 04-29-2006, 06:25 AM
  4. Seeking Format Advice
    By PsychoBrat in forum Game Programming
    Replies: 3
    Last Post: 10-05-2005, 05:41 AM
  5. Having trouble with operator*=()
    By Lurker in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2003, 02:03 PM

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