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

Dani AI

Generated

The two compiler messages (C4430 and C2511) come from a syntax error in the declaration and a mismatch between the declaration and the out-of-class definition. As suggested, the parameter type must appear before the reference qualifier (the compiler needs a concrete type token it can parse). If the class declaration and the Set::operator- definition do not match exactly (including any trailing const), the compiler treats the definition as a different, unknown function and emits C2511.

Checklist to resolve the immediate errors:

  • Fix the parameter syntax so the type is written before the & (or use Type const&).
  • Make sure the in-class declaration and the out-of-class definition are identical (same parameter types and same const qualification).
  • Look for earlier/preceding source problems (missing semicolon after a previous declaration, stray macros, or an unclosed brace) that can confuse the parser and produce cascading errors.
  • If the operator does not modify *this, declare it with a trailing const so the signatures truly match.

Other practical notes derived from 's code:

  • Because Set uses a raw pointer, ensure correct resource management (deep copy, proper destructor, correct assignment operator). Returning or filling a Set that has no allocated storage will cause runtime corruption; confirm the result object has capacity or use safe insertion functions. Follow the special-member rules to avoid shallow-copy bugs (see special member functions guidance on cppreference).
  • Consider using the STL to avoid low-level bugs. For integer sets, std::set<int> plus std::set_difference is concise and well-tested:
std::set<int> diff;
std::set_difference(A.begin(), A.end(), B.begin(), B.end(),
                    std::inserter(diff, diff.end()));

References: operator overloading rules and examples are documented on cppreference: https://en.cppreference.com/w/cpp/language/operators and the algorithm set_difference: https://en.cppreference.com/w/cpp/algorithm/set_difference.

Recommended Answers

All 3 Replies

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 Member #114696

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.