Thread: simple help!! I am failing with CLASS

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    16

    simple help!! I am failing with CLASS

    HI GUYS
    I am in big trouble!!

    I have so many problems it is hard to know where to begin. Firstly, I will show code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cassert>
    #include <iomanip>
    #include "date.h"
    using namespace std;
    
    int main()
    {
      date temp1;
      temp1.setDay(2);
      temp1.getDay();
      cout << temp1 << endl;
    }
    I have not yet attempted to link this to my library function, just trying to see if this works first...

    I am not sure because I am very naive with this but think that it is the way I am trying to compile it.

    This method has worked for me in the past.

    Is there anything wrong with the actual code?

    Here is a brief (compared to the huge running list of error msg) idea of what happens when I compile by

    Code:
    g++ myProgram.cpp -c
    The errors:
    Code:
    assign5.cpp: In function `int main()':
    assign5.cpp:19: error: no match for 'operator<<' in 'std::cout << temp1'
    /usr/include/c++/3.3.2/bits/ostream.tcc:63: error: 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>]
    /usr/include/c++/3.3.2/bits/ostream.tcc:85: error:
       std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
       _Traits>::operator<<(std::basic_ios<_CharT,
       _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits
       = std::char_traits<char>]
    /usr/include/c++/3.3.2/bits/ostream.tcc:107: error:
       std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
       _Traits>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT = char,
       _Traits = std::char_traits<char>]
    /usr/include/c++/3.3.2/bits/ostream.tcc:179: error:
       std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
       _Traits>::operator<<(long int) [with _CharT = char, _Traits =
       std::char_traits<char>]
    /usr/include/c++/3.3.2/bits/ostream.tcc:216: error:
       std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
       _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits =
       std::char_traits<char>]
    /usr/include/c++/3.3.2/bits/ostream.tcc:154: error:
       std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
       _Traits>::operator<<(bool) [with _CharT = char, _Traits =
       std::char_traits<char>]
    /usr/include/c++/3.3.2/ostream:178: error:
       std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
       _Traits>::operator<<(short int) [with _CharT = char, _Traits =
       std::char_traits<char>]
    /usr/include/c++/3.3.2/ostream:189: error:
       std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,

    Any ideas appreciated

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You need to define a operator<< for your date class. Put this inside your class defination:
    Code:
    friend std::ostream& operator<<(std::ostream&, const date&);
    And then with your other function definations you'll have to define it. It'll be something like:
    Code:
    std::ostream& operator<<(std::ostream& o, const date& d)
    {
      return o << d.month() << '/' << d.day() << '/' << d.year();
    }
    or however you want to format it.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    Thankyou!! I Will Try Now

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    It compiled!! Thankyou so much!! Of course, it is not doing what it should, but that I can figure out!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Class definition
    By Emeighty in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 02:55 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  4. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  5. Errors in a simple hash table class.
    By TheSquid in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2005, 04:49 AM