operator - (minus) overloading problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 61
Reputation: Eko is an unknown quantity at this point 
Solved Threads: 1
Eko's Avatar
Eko Eko is offline Offline
Junior Poster in Training

operator - (minus) overloading problem

 
0
  #1
Apr 13th, 2007
I have a class , named Set,and I want to overload the operator - so I can get the difference between 2 Set object.
i.e. A={1,2,3,4} , B={ 3,4,5,6} , A-B={1,2}

In header file I declared the operator function like this :

  1. Set operator-(const Set & ) ;
In the definition file :

  1. Set Set::operator -(const Set & a)
  2. {
  3. int dif,i,j,k;// some handy variables
  4. Set f; // the object who's gonna be returned by the function
  5. f.size=0;// setting the size to 0 for now
  6. for(i=0;i<size;i++)
  7. {
  8. dif=0;
  9. for(j=0;j<a.size;j++)
  10. {
  11. if(ptr[i]==a.ptr[j])
  12. ++dif;
  13. }
  14. if(dif==0)
  15. f.ptr[f.size++]=ptr[i]; //if an element wasn't found,in the //other set , assign it to the f object .
  16. }
  17. return f;
  18. }

In Visual Studio 2005 I'm getting
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
and
error C2511: 'Set Set::operator -(const Set &)' : overloaded member function not found in 'Set'

Last edited by Eko; Apr 13th, 2007 at 5:59 am.
"Get rich or die tryin"(50-cent)
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: operator - (minus) overloading problem

 
0
  #2
Apr 13th, 2007
Have you declared Set operator-(const Set & ) ; inside class.

Please post the class declaration.

Perhaps these may show the reason for error.
http://msdn2.microsoft.com/en-us/library/ms173696.aspx
http://msdn2.microsoft.com/en-us/library/20fs9x81.aspx
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 61
Reputation: Eko is an unknown quantity at this point 
Solved Threads: 1
Eko's Avatar
Eko Eko is offline Offline
Junior Poster in Training

Re: operator - (minus) overloading problem

 
0
  #3
Apr 13th, 2007
Originally Posted by vishesh View Post
Have you declared Set operator-(const Set & ) ; inside class.
As Snoop Dog would said , fo'shizzle

Here's the header file :
#ifndef SET_H
#define SET_H

#include <iostream>

using std::ostream;
using std::istream;


class Set
{
    friend ostream &operator<<( ostream &, const Set & );
    friend istream &operator>>( istream &, Set & );
    friend Set intersection(const Set & ,const Set & );
    friend Set reunioun(const Set & , const Set & );
    
    

public:

    Set( int = 10 ) ; // default constructor.
                          
    Set( const Set & )  ; // copy constructor
    ~Set() ;          // destructor
    int getSize() const ; // returns # of elements
    const Set &operator=( const Set & ); // assignment operator overloading
    bool operator==( const Set & ) const;  // equality operator overloading

   bool operator!=( const Set &right ) const  // != operator
      { return ! ( *this == right ); }


    int   &operator[](   int   );                // overloading [] subscript
    const   int  &operator[](  int  )  const ;   // =====/ / / ==== for const objects  


  Set operator-(const & Set); // Here the compiler shows errors

     
    static int  getSetCount();         //  returns the # objects instantiated
    
    // some set operations
    bool apartenenta(int ) ; 
    bool incluziunea  (const Set & ) const ; 
    bool egalitatea() ; 
    

    

private:
        int  size;  // the size of vector
        int  *ptr;  // pointer to the first element of the vector
        static int  count;   // it retaines the # of objects instantiated
Originally Posted by vishesh View Post
Perhaps these may show the reason for error.
http://msdn2.microsoft.com/en-us/library/ms173696.aspx
http://msdn2.microsoft.com/en-us/library/20fs9x81.aspx
Those links didn't help.

The funny thing is , that almost every person I asked about (most of them students in my class) , had encountered this problem with operator- overloading,but they didn't know the solution
It feels like a mysterious thing...
Last edited by Eko; Apr 13th, 2007 at 8:31 am.
"Get rich or die tryin"(50-cent)
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: operator - (minus) overloading problem

 
0
  #4
Apr 13th, 2007
Change this
  1. Set operator-(const & Set);
to this
  1. Set operator-(const Set&);


Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC