Overloading + operator on generic class

This is a discussion on Overloading + operator on generic class within the C++ Programming forums, part of the General Programming Boards category; Hi all. I'm trying to overload the + operator in a forest class, a forest being a collection of trees, ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    Overloading + operator on generic class

    Hi all. I'm trying to overload the + operator in a forest class, a forest being a collection of trees, and the + operator is supposed to combine two forests into one. I have the following code as my class definition:


    Code:
    template<typename NODETYPE>
    class Forest
    {
    
    
        public:
            
            friend Forest& operator+<>(Forest&, Forest&);
            friend ostream& operator<<<>(ostream&, const Forest&);
            friend istream& operator>><>(istream&, Forest&);
            Forest();
            Forest( const Forest& otherForest);
            ~Forest();
            void nodes(int&) const;
    
        private:
            ForestNode<NODETYPE> *root;
    
            ForestNode<NODETYPE> *getNewNode( const NODETYPE &);
    };

    The following is my implementation of operator+:


    Code:
    template<typename NODETYPE>
    Forest& operator+<>(Forest& f1, Forest& f2)
    {
        f3 = new Forest();
        f3.root = *f1.*root;
        f3.root.sibling = *f2.*root;
        *f1.root = 0;
        *f2.root = 0;
        return f3;
    }
    I get the following error on compile:

    |28|error: expected constructor, destructor, or type conversion before '&' token|
    line 28 refers to the signature of my operator+ implementation.

    I think to correct it i am supposed to add <NODETYPE> to the return type, giving:

    Code:
    template<typename NODETYPE>
    Forest<NODETYPE>& operator+<>(Forest& f1, Forest& f2)
    {
        f3 = new Forest();
        f3.root = *f1.*root;
        f3.root.sibling = *f2.*root;
        *f1.root = 0;
        *f2.root = 0;
        return f3;
    }
    But that gives me the following errors:


    |28|error: declaration of 'operator+' as non-function|
    |28|error: missing template arguments before '&' token|
    |28|error: 'f1' was not declared in this scope|
    |28|error: missing template arguments before '&' token|
    |28|error: 'f2' was not declared in this scope|

    Can anyone help me with this? I'd be very very thankful.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    1) Take out extraneous <> characters in the friend declarations

    2) The definition of the operator+() will look like
    Code:
    template<typename NODETYPE>
    Forest<NODETYPE> operator+(Forest<NODETYPE>& f1, Forest<NODETYPE>& f2)
    {
        Forest<NODETYPE> f3;   //   don't dynamically allocate
    
    }
    3) It is conventional that operator+() return a separate object (not a reference) and that the arguments supplied to it are const.
    Last edited by grumpy; 10-27-2010 at 06:53 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    I don't think you need the <> after the + and before the (). But then I have not done a template overloaded operator before.

    Also wouldn't it be:
    Code:
    Forest<NODETYPE>& operator+(Forest<NODETYPE>& other,)
    I have used it in this format but not passing two parameters.
    Last edited by fadlan12; 10-27-2010 at 06:58 AM. Reason: I Type too slow. Grumpy beat me to it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix Class Operator Overloading.
    By Fatima Rizwan in forum C++ Programming
    Replies: 6
    Last Post: 09-15-2010, 10:59 AM
  2. desiging generic message class
    By KIBO in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2010, 05:15 AM
  3. Question about design class..
    By ovid in forum C++ Programming
    Replies: 5
    Last Post: 03-17-2010, 11:34 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Problems: Operator overloading.
    By Dual-Catfish in forum C++ Programming
    Replies: 17
    Last Post: 06-18-2002, 07:38 PM

Tags for this Thread


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