Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ Error with Classes

Started by Lorek Nov 8, 2004 at 11:54 PM 3 replies 1.1k views
Original Post
Lorek
Lorek
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 #include //-------------------------------------------------------- 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(); } //---------------------------------------------------------
Bincho
Bincho
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;
auLucifer
auLucifer
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
Lorek
Lorek
Thanks for the quick reply.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.