Hi,
Trying to overload - and getting this error. I am not sure why..

error C2447: missing function header (old-style formal list?)

#include<iostream.h>
#include<string>
#include<new>
#include<ctime>
using namespace std;
 
class D
{
public:
 D operator- (const D &d2);
};
 
 
D D::operator- (const D &d2);
{  //error points to here!!
cout<<"wont work....";
}

:-/ using VS C++ 6.0

Recommended Answers

All 20 Replies

Member Avatar for iamthwee

vc++ 6.0? Er why?

>D D::operator- (const D &d2);
You have a rogue semicolon.

vc++ 6.0? Er why?

why not?..:|

>D D::operator- (const D &d2);
You have a rogue semicolon.

:-O ... i knew i was overlooking something...thanks :)

>why not?..
Because VC++ 6 was released before the C++ standard was ratified. It has serious conformance issues and will refuse to compile perfectly legitimate C++ code. I spent more time hacking the damn thing to get it to work right than I did getting any work done. I strongly suggest you get Visual Studio 2005.

class D
{
   // ...
   D operator- ( const D& d2 ) const;
   // ...
};
D D::operator- ( const D& d2 ) const
{
  // ...

would also make your code const-correct.

>Because VC++ 6 was released before the C++ standard was ratified.
o, I didnt know that..thanks

>would also make your code const-correct.
thanks, wasnt sure about that before.


another question :
Can I compare more than 1 things inside the overloaded functions?
eg:

D D::operator- (const D &d2) const
{
 if ((day<d2.day) && (month=d2.month))
  day=(d2.day-day);

if so how would i get the users input? and how would i find the difference?

D x;
cout<<"Day:";
 cin>>day;
cin>>x.day;
 
day=(x.day).operator- (day); //is not correct

im not sure how..
please help

>Can I compare more than 1 things inside the overloaded functions?
It's a function. You can do anything you want as long as you maintain the correct interface for the overloaded operator.

>if so how would i get the users input? and how would i find the difference?
If I understand correctly, you would get the user's input by overloading the >> operator and doing something like this:

D d1, d2;

cin>> d1 >> d2;

d1 = d1 - d2;

> day=(x.day).operator- (day); There is a reason we do operator overloading. Don't you think the code above looks a bit illogical.

Read up on some good operator overloading tutorials to actually understand what it is used for. And BTW, operator overloading is used as: obj1 = obj2 - obj3; where all objects are instances of the same class and you have specifically overloaded the minus(-) operator.

>There is a reason we do operator overloading. Don't you think the code above looks a bit illogical.
Really? That is why im using operator overloading here...
I know its illogical..read the comment after it!
I was a bit confused before so was looking for clarification.

Narue is pointing in the right direction.Thanks !
I know what you mean. :)

a little trouble with using the operator- with overloaded <<..not sure how to do it, together or separate? i want to get the difference between 2 objects using operator overloading.

istream& operator >> (istream& in, D& a)
 {
  int d1,d2;
 cout<<"Enter the day of the first object:"<<endl;
 cout<<"Day:";
 in>>d1;
 a.d1=day;
 
 cout<<"Enter the day,of the second object you want to find the difference from:"<<endl;
 cout<<"Day:";
 in>>d2;
 b.d2=day;
  return in;
 }
 
 ostream& operator << (ostream& out, D& a)
 {out<<"The days are: "<<(a.day1)-(a.day)<<endl; [B]//here i want to use the overloaded operator- and im not sure how?[/B]
  return out;
 }

>b.d2=day;
Where did 'b' come from?

I think you're trying to do too much in your operator overloading. How I would write it is for the >> operator I would just grab a value off of the input stream, and send this into one of the variables in 'a'. For the << operator, I would just output the values contained inside 'a'.

Then you can worry about using the overloaded '-' in the function that calls it. And your code is much clearer.

>Where did 'b' come from?
Ya sorry thats a.

Well thats where im stuck, i guess i dont need the << operator.
For the >> operator how would i find the difference between
(a.day1)-(a.day)
Im not sure how to call the operator- using the values from operator>> ?
day1.operator- (day)?? please help

istream& operator >> (istream& in, D& a) { 
 int d1,d2; 
cout<<"Enter the day of the first object:"<<endl; 
 in>>d1; 
a.day=d1; 
 
 cout<<"Enter the day of the second object you want to find the difference from:"<<endl; 
 in>>d2; 
a.day1=d2;  
return in; }

> For the >> operator how would i find the difference between
> (a.day1)-(a.day)

Um, they're both integers. So you would write a.day1-a.day; , just like you would subtract any two integers from each other.

thanks i get that
but what i still dont get is :
Im not sure how to call the operator- using the values from operator>> ?

what im trying to do is find the difference between the 2 values i get from operator>> using operator-
i dont knw i might be trying to do something wrong here. all i want to do is find the difference between 2 values using operator-

void D::difference()
{
D a1,d,d1;
cin>>a1;
d=(a1).operator- (d1);
cout<<d;
}

>Im not sure how to call the operator- using the values from operator>> ?
Don't, because you only have control of one of the day objects within the overloaded >> operator.

What you need to do is simplify the overloading to just grabbing the value (all code is untested):

istream& operator >> (istream& in, D& a) {
    
    in >> a.day;
    return in;
}

Now, from another function, you would do something like this:

D object1, object2, object3;

cout << "Enter the day of the first object:" << endl;
cin >> object1;
cout << "Enter the day of the second object you want to find the difference from:" << endl;
cin >> object2;

object3 = object1-object2;

O I see it now..:)
but for that, the operator- has to be friend also
im currently not usingit as friend. have to change all of it, unless theres someother way?

Thanks a lot for your help .
Really very helpfull!

>but for that, the operator- has to be friend also
No... operator- would be an actual member of class 'D', and has full access rights. What _would_ need to be a friend is operator<< and operator>>, because they are not members of class 'D', and they both need access to the private member D::day .

yes thanks, i was just going to edit my post. I realised that later.
so my function can be something like this?(to check if month and year are same) i tried that but it doesnt work.

D D::operator- (const D &d2)
{
if ((day<d2.day) && (month=d2.month) && (year=d2.year))
day=(d2.day-day);
}

= is the assignment operator, and is basically used for assigning values to variables. What you need is the comparison operator == to compare values.

Thanks s.o.s
Not a good day for me..making too many silly mistakes..:(..I put == for the rest, just missing in these lines...

My best of regards to Joe and everyone else. Appreciate the help!

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.