| | |
operator - (minus) overloading problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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 :
In the definition file :
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'

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)
Set operator-(const Set & ) ;
C++ Syntax (Toggle Plain Text)
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'

Last edited by Eko; Apr 13th, 2007 at 5:59 am.
"Get rich or die tryin"(50-cent)
Have you declared
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
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
As Snoop Dog would said , fo'shizzle 
Here's the header file :
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...

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•
•
•
•
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
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)
Change this to this
C++ Syntax (Toggle Plain Text)
Set operator-(const & Set);
C++ Syntax (Toggle Plain Text)
Set operator-(const Set&);
![]() |
Similar Threads
- Constructor overloading problem (VB.NET)
- overloading problem (C++)
- Please some one write this program (C++)
- C++ Tic Tac Toe using classes & operator overloading (C++)
- overloading Operator << to accept endl (C)
Other Threads in the C++ Forum
- Previous Thread: iomanip, setprecision - inprecise?
- Next Thread: Symbian application C++
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






