944,050 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4347
  • C++ RSS
Apr 13th, 2007
0

operator - (minus) overloading problem

Expand Post »
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 :

C++ Syntax (Toggle Plain Text)
  1. Set operator-(const Set & ) ;
In the definition file :

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Eko
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
Eko is offline Offline
60 posts
since Nov 2006
Apr 13th, 2007
0

Re: operator - (minus) overloading problem

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
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Apr 13th, 2007
0

Re: operator - (minus) overloading problem

Click to Expand / Collapse  Quote originally posted by vishesh ...
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
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.
Eko
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
Eko is offline Offline
60 posts
since Nov 2006
Apr 13th, 2007
0

Re: operator - (minus) overloading problem

Change this
C++ Syntax (Toggle Plain Text)
  1. Set operator-(const & Set);
to this
C++ Syntax (Toggle Plain Text)
  1. Set operator-(const Set&);


Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with C++ project
Next Thread in C++ Forum Timeline: Symbian application C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC