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 :

Set operator-(const Set & ) ;

In the definition file :

Set Set::operator -(const Set & a)
    {
         int dif,i,j,k;// some handy variables
         Set f; // the object who's gonna be returned by the function
         f.size=0;// setting the size to 0 for now 
         for(i=0;i<size;i++)
        {  
            dif=0;
            for(j=0;j<a.size;j++)
            {      
                if(ptr[i]==a.ptr[j])
                    ++dif;
            }
         if(dif==0) 
             f.ptr[f.size++]=ptr[i]; //if an element wasn't found,in the    //other set , assign it to the f object .
        }
            return f;
    }

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'
:'(

Have you declared Set operator-(const Set & ) ; inside class.

As Snoop Dog would said , fo'shizzle :cool:

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  


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

[/B]     
    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

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...

Member Avatar for GreenDay2001

Change this

Set operator-(const & Set);

to this

Set operator-(const Set&);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.