whats wrong with the following code?

This is a discussion on whats wrong with the following code? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <stdlib.h> #include <iostream> #include <fstream> #include <list> using namespace std; ostream& M::print(ostream& stream) { s::iterator i; for(i = ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    whats wrong with the following code?

    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    #include <list>
    using namespace std;
    
    ostream& M::print(ostream& stream) {
            s::iterator i;
            for(i = t->begin(); i != t->end(); ++i)
            {
                    stream << *i;
            }
    
    }
    ?

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Where is pointer t defined and what is the error message?
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    t is defined in the header file
    Code:
    .
    .
    .
    private:
      typedef list<ML> s;
            s * t;
    and i get a huge list of errors
    Code:
    M.cpp: In member function `std::ostream&
       M::print(std::ostream&)':
    M.cpp:56: error: no match for 'operator<<' in 'stream <<
       (&i)->std::_List_iterator<_Tp, _Ref, _Ptr>::operator*() const [with _Tp =
       ML, _Ref = ML&, _Ptr = ML*]()'
    /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>]
    .
    .
    .
    list goes on..

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Iīm not sure but shouldn't M::print() also be templated??
    Last edited by ripper079; 08-03-2004 at 03:20 AM.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    not sure..
    but i've realised that when i have
    Code:
    ostream& M::print(ostream& stream) {
            s::iterator i;
            for(i = t->begin(); i != t->end(); ++i)
            {
                    stream << *i;
            }
    }
    that gives me a bunch of errors
    but if i have
    Code:
    ostream& M::print(ostream& stream) {
            s::iterator i;
            for(i = t->begin(); i != t->end(); ++i)
            {
                    stream << "hello";
            }
    }
    that gives me no problems
    so im thinking that it could be the *i...
    i've just been searching aroudn and i foudn this
    Code:
    if (! i->passed())    // iterators also provide operator ->
          {
             cout << "The student " << *i << " failed." << endl;
              // provided that class Student provides the overloaded
              // stream insertion operator <<
          }
    not sure what that means tho..

  6. #6
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    just out of curosity what happen when you instantiate your class (M) template with a primitive datatype?
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    what do you mean? i dont understand..
    im pretty new to c++

  8. #8
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Hmm, pretty new to c++...I donīt want to be rude but templates (particular class) shoudlnīt be learned to a "im pretty new to c++" in c++. You canīt run if you canīt walk . But I will make a try.

    Primitive datatypes: Are datatypes that are built-in in c++ e.i int, float, double, char...

    Template instantiation: Is when a function or a class is substituted with a buildt-in- or an user defined type.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    hmmm yeah you've lost me.. haha
    but now i'm defintely sure that the probelm is
    when i do the printout of *i

  10. #10
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    i is the iterator for your list, which is of type ML. The ostream class doesn't know how to handle the '<<' operator with that class unless you define it. You'll need to overload the '<<' operator to work with an ostream and your ML class, or go about it by calling a member function of the ML class that will just print it's data types or something.

    The FAQ and tutorials section will tell you how to go about overloading the '<<' operator (or the insertion operator in this case, if you're curious about a formal name...not to be confused with the bitshif left operator).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 05:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 01:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21