Sign in to follow this  
Lorek

C++ Error with Classes

Recommended Posts

I've been trying to get an exercise in my book to work but I keep recieving a error. Even though my source is almost identical to the answer in the back of the book with the exception of different var names. C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion) The problem i'm working from is in the Sams Robert Lafore book, Object Oriented Programming Version 4, Chapter 6 Exercise 3. Create a class called time that has separate int member data for hours, minutes, and seconds. One constructor should initialize this data to 0, and another should initialize it to fixed values. Another member function should display it, in 11:59:59 format. The final member function should add two objects of type time passed as arguments. //------------------------------------------------------------- My Code //------------------------------------------------------------- #include <iostream.h> #include <conio.h> //-------------------------------------------------------- class time { private: int hours, minutes, seconds; public: time() : hours(0), minutes(0), seconds(0) {} time(int h, int min, int sec) : hours(h), minutes(min), seconds(sec) {} void display() { cout << "Time: " << hours << ":" << minutes << ":" << seconds; } void add_time(time a, time b) { seconds=a.seconds+b.seconds; minutes=a.minutes+b.minutes; hours=a.hours+b.hours; if(seconds> 59) { minutes++; seconds-=60; } if(minutes> 59) { hours++; minutes-=60; } } }; //-------------------------------------------------------- void main() { time a(1,1,50); time b(2,59,40); time c; c.add_time(a, b); cout << "time c = " << c.display(); cout << endl; getch(); } //---------------------------------------------------------

Share this post


Link to post
Share on other sites
Your problem is that the display() member function doesn't return a value. The statement in main() where you add it to a sequence of statements sent to cout expects a value it can use. You should put c.display() on it's own line, as follows:

c.add_time(a, b);

cout << "time c = ";
c.display();
cout << endl;

Share this post


Link to post
Share on other sites
Your problem appears to be right at the end.
You are trying to output c.display()'s return value which is void.

edit: Damn proxy. You just beat my reply. ;)
Except yours happens to contain a little more detail

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this