Question about the this pointer

S

Schizoid Man

Hello,

Suppose I have a class called ModDate that contains three private
members - day, month, year - all defined as int.

I want to overload the += operator and add an integer value to the date
to get the new date after increments.

To do that I have the following method:

const ModDate &ModDate::eek:perator+=(int addDays) {
for (int i = 0; i < addDays; i++)
incrementDay();
return *this;
}

However, when I call this method in the following lines, I get compile
error C2676 binary '<<' : no operator found which takes a right-hand
operand of type 'const ModDate' (or there is no acceptable conversion)
with Visual C++.

ModDate enDate1;
enDate1.setDate(1, 1, 2001);
cout << "After increment: " << (enDate1 += 7);

I would appreciate any advice.

Thanks.
 
B

benben

Schizoid said:
Hello,

Suppose I have a class called ModDate that contains three private
members - day, month, year - all defined as int.

I want to overload the += operator and add an integer value to the date
to get the new date after increments.

To do that I have the following method:

const ModDate &ModDate::eek:perator+=(int addDays) {
for (int i = 0; i < addDays; i++)
incrementDay();
return *this;
}

However, when I call this method in the following lines, I get compile
error C2676 binary '<<' : no operator found which takes a right-hand
operand of type 'const ModDate' (or there is no acceptable conversion)
with Visual C++.

ModDate enDate1;
enDate1.setDate(1, 1, 2001);
cout << "After increment: " << (enDate1 += 7);

If you haven't defined operator<< how the hell the compiler's gonna know
how to output your date?
I would appreciate any advice.

Thanks.

Ben
 
I

Ian Collins

Schizoid said:
Hello,

Suppose I have a class called ModDate that contains three private
members - day, month, year - all defined as int.

I want to overload the += operator and add an integer value to the date
to get the new date after increments.

To do that I have the following method:

const ModDate &ModDate::eek:perator+=(int addDays) {
for (int i = 0; i < addDays; i++)
incrementDay();
return *this;
}

However, when I call this method in the following lines, I get compile
error C2676 binary '<<' : no operator found which takes a right-hand
operand of type 'const ModDate' (or there is no acceptable conversion)
with Visual C++.

ModDate enDate1;
enDate1.setDate(1, 1, 2001);
cout << "After increment: " << (enDate1 += 7);
Nothing to do with the this pointer, looks like you haven't defined a <<
operator for ModDate.
 
S

Schizoid Man

Ian said:
Nothing to do with the this pointer, looks like you haven't defined a <<
operator for ModDate.

Hi Ian,

I now understand the necessity to post complete code. My code the
overloading of the << operator is included below.

I did make a modification to the original overloading declaration. Now,
using it as

const ModDate &ModDate::eek:perator+(int addDays)

and calling it in the following manner

enDate1.setDate(1, 1, 2001);
enDate1 = enDate1 + 7;
cout << enDate1;

seems to work just fine. Once I got rid of the = sign, it worked as I
expected. Unfortunately with the = sign back, I still get the same
compile error.

Thanks,
Schiz

ostream &operator<< (ostream &strm, ModDate &date) {
switch(date.month) {
case 1:
strm << "January";
break;
case 2:
strm << "February";
break;
case 3:
strm << "March";
break;
case 4:
strm << "April";
break;
case 5:
strm << "May";
break;
case 6:
strm << "June";
break;
case 7:
strm << "July";
break;
case 8:
strm << "August";
break;
case 9:
strm << "September";
break;
case 10:
strm << "October";
break;
case 11:
strm << "November";
break;
case 12:
strm << "December";
break;
default:
strm << " ";
break;
}
 
K

Kai-Uwe Bux

Schizoid said:
Hi Ian,

I now understand the necessity to post complete code. My code the
overloading of the << operator is included below.

I did make a modification to the original overloading declaration. Now,
using it as

const ModDate &ModDate::eek:perator+(int addDays)

and calling it in the following manner

enDate1.setDate(1, 1, 2001);
enDate1 = enDate1 + 7;
cout << enDate1;

seems to work just fine. Once I got rid of the = sign, it worked as I
expected. Unfortunately with the = sign back, I still get the same
compile error.

Thanks,
Schiz

ostream &operator<< (ostream &strm, ModDate &date) {
switch(date.month) {
case 1:
strm << "January";
break;
case 2:
strm << "February";
break;
case 3:
strm << "March";
break;
case 4:
strm << "April";
break;
case 5:
strm << "May";
break;
case 6:
strm << "June";
break;
case 7:
strm << "July";
break;
case 8:
strm << "August";
break;
case 9:
strm << "September";
break;
case 10:
strm << "October";
break;
case 11:
strm << "November";
break;
case 12:
strm << "December";
break;
default:
strm << " ";
break;
}

The operator<< does not take a const reference. Therefore, the return type
of operator+= does not match.


Best

Kai-Uwe Bux
 
K

Kai-Uwe Bux

Schizoid said:
Hello,

Suppose I have a class called ModDate that contains three private
members - day, month, year - all defined as int.

I want to overload the += operator and add an integer value to the date
to get the new date after increments.

To do that I have the following method:

const ModDate &ModDate::eek:perator+=(int addDays) {
for (int i = 0; i < addDays; i++)
incrementDay();
return *this;
}

This is probably unrelated to your problem, however: are you sure about the
int parameter? It suggests that negative values are permissible inputs. For
those, the natural interpretation would be that the date is decremented,
but the code will not do that.
However, when I call this method in the following lines, I get compile
error C2676 binary '<<' : no operator found which takes a right-hand
operand of type 'const ModDate' (or there is no acceptable conversion)
with Visual C++.

ModDate enDate1;
enDate1.setDate(1, 1, 2001);
cout << "After increment: " << (enDate1 += 7);


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top