C++ Error with Classes

Started by
3 comments, last by Roboguy 19 years, 5 months ago
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(); } //---------------------------------------------------------
Advertisement
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;
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
Thanks for the quick reply.
also, according to the ISO/ANSI C/C++ standards, main MUST return int, some compilers won't even compile code that has a main that returns void.

This topic is closed to new replies.

Advertisement