Thread: Don't Really Get What's Wrong Here...

  1. #1
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    Don't Really Get What's Wrong Here...

    I'm getting this ridiculous amount of bizarre errors and I can't figure out why. I have 2 classes and a main file I'm working with here... One class is called "Date" and the other is "Homework". The Homework class makes use of the Date class, which has already been tested and works fine.

    The problems seems to be coming from when I overloaded the << operator for "Homework". Not so much the signature or anything, but what I have inside of that method.

    These are some of the errors I get: The other errors seem to be just repeats or warnings.

    Code:
    Homework.h: In function ‘std::ostream& operator<<(std::ostream&, Homework&)’:
    Homework.h:92: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits<char>]
    (((std::basic_ostream<char, std::char_traits<char> >&)(+ output)), ((const char*)", assigned ")) << (+ homework)->Homework::getAssignedDate()’
    
    /usr/include/c++/4.0.2/bits/ostream.tcc:67: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::
    operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&))
     [with _CharT = char, _Traits = std::char_traits<char>]

    Here's my main file:
    Code:
    #include <iostream>
    #include "Date.h"
    #include "Homework.h"
    
    using namespace std;
    
    int main(void){
    	Date assigned(1,1,2006);
    	Date due(4,1,2006);
    	
    	Homework ex1("exercise 1", assigned, due);
    
    //This line seems to be causing all of the problems.
    	cout << ex1;
    
    	cin.get();
    	return 0;
    }
    My definition of operator<< in class "Homework"
    Code:
    ostream& operator<<(ostream& output, Homework& homework){
    	output << homework.getName();
    	output << ", assigned " << homework.getAssignedDate();
    	output << ", due " << homework.getDueDate() << ".";
    	
    	return output;
    }
    In case it helps to know, I declare the operator<< function correctly in "Homework" with this signature:

    Code:
    friend ostream& operator<<(ostream& output, Homework& homework);
    Anybody have any ideas of what's wrong here? Because I'm stumped. Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Can you show the full Homework.h file? Can you show the operator<< for the Date class?

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The insertion operator shouldn't be a friend. It should be a function outside the scope of the class even. The compiler knows what inserter to call, because it has to; it looks at the arguments, like any other function.

    The Homework type that you pass in should also be const since an inserter should not change the type. Google it to learn how to make good inserters.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Why shouldn't it be a friend, whats your reasoning for that?
    Woop?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In this case, only (presumably) public functions are called inside the operator, so it doesn't require access to the internals of the class. Using friend is preferred over making it a member (which isn't really useful in the operator<< case), but making it a non-friend, non-member is even better.

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Well there are some occasions that using a friend would be a more viable option than using a non-friend. I was just wondering why he was saying that you "shouldn't" use a friend for an insert operator<<.

    >Google it to learn how to make good inserters.
    IMO his inserter is fine, of course const is prefered but it not required to make it a "good" inserter.
    Woop?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I agree, and almost mentioned it myself. There are many cases when it is a good decision to make it a friend.

    If you find yourself exposing internal data through a Get method so that the operator<< can access it without being a friend, then you should just go ahead and make it a friend instead. The operator<< is part of the class interface, even if it is not a member. Adding a public Get function exposes internal data to everyone, while making operator<< a friend only exposes internal data to that one function that you are writing and including with the class yourself.

  8. #8
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    This seems to have turned into more of a debate about when and when not to use 'friend', as opposed to hints on what I might be screwing up here. But apparently a 'friend' method isn't actually a member of the class? Hmm...I don't really see many advantages to that. In my comp class, we were just told to use 'friend' for overloading the insertion and extraction operators...not very helpful.

    So maybe if I make this a member of "Homework" it may work better? I kind of suck with this 'friend' key word. I haven't learned a whole lot about it.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> So maybe if I make this a member of "Homework" it may work better?
    No, you have it right. We're just waiting for your response to my earlier questions:

    Can you show the full Homework.h file? Can you show the operator<< for the Date class?

  10. #10
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Well, There is nothing wrong with your operator << from eyeballing it. Someone asked for more code from above. friend basically means that the object declared as a friend can touch your class's naughty parts and the compiler won't complain.
    Woop?

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    its probable that getDate() returns something for which there is no overloaded operator<< but we cant tell without seeing that code

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    What does getAssignedDate() return? A Date object? Because the compiler is complaining about operator<<() being not implemented for that type...
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  13. #13
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    The declaration of the class is as follows:

    Code:
    #include <iostream>
    using namespace std;
    
    #ifndef DATE_H
    #define DATE_H
    
    /*----------------------------*
     * Author: Dean Angerer
     * Date: 05/08/2006
     * Version: 1.0.0
     *
     * Models a date of the year
     *---------------------------*/
    
    class Date {
    	
    private:
    
    	int day;
    	int month;
    	int year;
    
    
    public:
    
    	
    	/**
    	 * Default constructor that initializes
    	 * day, month, and year to zero.
    	 */
    	Date(): day(0), month(0), year(0) {}
    
    
    	/**
    	 * Constructor that provides day, month, 
    	 * and year paramters
    	 *
    	 * @param d - day to set date to
    	 * @param m - month to set date to
    	 * @param y - year to set date to
    	 */
    	Date(int d, int m, int y) : day(d), month(m), year(y) {}
    
    	
    	/**
    	 * These 3 functions respectively return
    	 * day, month, and year
    	 */
    	int getDay() { return day; }
    	int getMonth() { return month; }
    	int getYear() { return year; }
    
    
    	/**
    	 * These 3 functions respectively set
    	 * day, month, and year to new values
    	 */
    	void setDay(int d) { day = d; }
    	void setMonth(int m) { month = m; }
    	void setYear(int y) { year = y; }
    
    
    	/**
    	 * Function that compares two dates
    	 * 
    	 * @return - relationship between two dates
    	 *
    	 *	     Less than 0 means the first date is smaller (less recent)
    	 *	     Zero means the dates are equal
    	 *	     More than 0 means the first date is larger (more recent)
    	 *
    	 * @param anotherDate - Another date to compare current date to.
    	 */
    	int compareTo(Date anotherDate);
    
    
    	/**
    	 * Dictates how the date is to be printed in the output stream
    	 *
    	 * @param output - reference to an output stream object (typically cout)
    	 * @param date - reference to a Date object to be printed out
    	 *
    	 * @return - reference to output stream object after the Date information
    	 *           has been inserted into it.
    	 */
    	friend ostream& operator<<(ostream& output, Date& date);
    };

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Can you show the full Homework.h file? Can you show the operator<< for the Date class?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM