0

When I try my own version I am getting

error C2039: 'endl' : is not a member of 'abc' error C2065: 'endl' : undeclared identifier

Here is the code below.

#include <iostream>
#include <stdio.h>

//assume this class uses some proprietary logging system.  I just use the wrap the C
//output functions here but assume is a corporate log system
namespace abc {

   class log_stream
   {
   public:
      log_stream(const char* filename) {
         fp_ = fopen(filename, "w");
      }

      log_stream& operator<<(short val)          { if (fp_) { output_int(val); } return *this; }
      log_stream& operator<<(unsigned short val) { if (fp_) { output_int(val); } return *this; }
      log_stream& operator<<(int val)            { if (fp_) { output_int(val); } return *this; }
      log_stream& operator<<(unsigned int val)   { if (fp_) { output_int(val); } return *this; }
      log_stream& operator<<(long val)           { if (fp_) { output_int(val); } return *this; }
      log_stream& operator<<(unsigned long val)  { if (fp_) { output_int(val); } return *this; }
      log_stream& operator<<(const char* val)    { if (fp_) { output_string(val); } return *this; }
      inline log_stream& endl(log_stream& os)  { return os.endl(); }
      //etc
      log_stream& endl() {
         if(fp_)
            fputc('\n', fp_);
         return *this;
      }

   private:
      void output_int(long v) { fprintf(fp_, "%d", v); }
      void output_string(const char* s) { fprintf(fp_, "%s", s); }
      FILE* fp_;
   };
}  //namespace abc

int main() {
   abc::log_stream logger("myfile.txt");
   //error C2039: 'endl' : is not a member of 'abc', error C2065: 'endl' : undeclared identifier
   logger << "number " << 3 << abc::endl;
   return 0;
}

UPDATE:

If I add

inline log_stream& endl(log_stream& os)  { return os.endl(); }

inside the abc namespace (but outside the class, I then get

error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'abc::log_stream' (or there is no acceptable conversion)

Which is closer (I think) to solving, but not there yet.

UPDATE2.

Well, that was hairy! Here is how I fixed problem for anyone doing anything similar. Thanks Julien.

added these to class:

  ~log_stream() { if(fp_) fclose(fp_); }

  // this is the main one I was missing
  abc::log_stream& abc::log_stream::operator<<( log_stream& (*pf)(log_stream&) )
  {
     return pf(*this);
  }

Then outside class:

abc::log_stream& endl( abc::log_stream& log ) { log.endl(); return log; }

4

2 回答 2

1

std::endl是一个以 astd::ostream作为其单个参数的函数,并且ostream<<是一个重载,其类型为std::endl.

要为您的 复制此行为,请log_stream覆盖std::endl它:

abc::log_stream& std::endl( abc::log_stream& log )
{
    log.endl();
    return log;
}

并覆盖abc::log_stream::operator<<以使用它:

abc::log_stream& abc::log_stream::operator<<( log_stream& (*pf)(log_stream&) )
{
    return pf(*this);
}

如您所见,参数 ofabc::log_stream::operator<<将类型std::endl作为参数。

另外,请注意std::endl 刷新流。因此,您应该考虑调用fflush(fp_).abc::log::endl()

于 2013-09-27T15:16:11.173 回答
0

I think you should make an operator<< that takes your endl type. Or let it take std::endl. See also this answer to a similar question: https://stackoverflow.com/a/1134467/4323 - basically, endl is a function like you have, but you need to make an insertion operator for your class that accepts that function as its right-hand argument, and invokes it.

于 2013-09-27T14:43:40.093 回答