Topic: Could anyone explain me why I get this compiler error?


Author: "Rodrigo de Salvo Braz" <Rodrigo_Braz@brown.edu.nospam>
Date: 2000/05/01
Raw View
Hi,

I have this (simplified from something real) and get the message:

[C++Error] Project1.cpp(10): 'operator<<' not implemented in type
'std::basic_ostream<char,std::char_traits<char> >' for arguments of type
'my_library::my_class::sub_class'.

I have no idea why that happens, especially because I DO define the
function.
Notice that if I define the struct 'sub_class' *outside* 'my_class' then
everything is fine.
Another way of fixing this is including 'write_container' inside namespace
'my_library'.
Is this a compiler bug?

Thanks in advance! Rodrigo

#include <list>
#include <iostream>

#define sub_class_inside_my_class // comment this out to have 'sub_class'
defined outside of 'my_class' without rewriting anything

template <class Container, class charT>
std::basic_ostream<charT>& write_container (std::basic_ostream<charT>& o,
const Container& c)
{
    Container::const_iterator it = c.begin();
    return o << (*it);
}

namespace my_library
{

using namespace std;

#ifndef sub_class_inside_my_class

struct sub_class
{ size_t i; };

class my_class {};

typedef sub_class the_sub_class;

#else

class my_class
{
    public:
    struct sub_class
    { size_t i; };
};
typedef my_class::sub_class the_sub_class;
#endif

ostream& operator<< (ostream& o, const the_sub_class& s1)
{ return o << s1.i << endl; }

void my_function()
{
    list<the_sub_class> l;
    the_sub_class s1;
    l.push_back(s1);
    write_container(cout, l);

}

} // end of namespace my_library

void main()
{
    my_library::my_function();
}





      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]