日期:2014-05-16  浏览次数:20651 次

关于类basic_ostream的构造函数被申明成protected
basic_ostream有两个构造函数:
public:
      explicit
      basic_ostream(__streambuf_type* __sb)
      { this->init(__sb); }
protected:
      basic_ostream(){this->init(0);}
 
在自己的类Trace中申明了
streambuf *streamBuffer_;
Trace::Trace(const ostream &out):
        streamBuffer_(out.rdbuf())
        {}

Trace trace(cout);

编译的时候报错了:
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream: In constructor 'Trace::Trace(const std::ostream&)':
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:361: error: 'std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char, _Traits = std::char_traits<char>]' is protected
不懂怎么解决了,Trace的构造函数该怎么样申明? 

非常感谢您的帮助!
------最佳解决方案--------------------
代码全一点看看。
不像是构造的问题,是否什么地方直接在对象上呼叫构造函数哦

------其他解决方案--------------------
引用:
代码全一点看看。
不像是构造的问题,是否什么地方直接在对象上呼叫构造函数哦


相关的就这几句,使用了Trace trace(cout);这样的话会调用Trace::Trace(const ostream &out)引发错误。   
既然Lib这么改动了  应该是会有方法的,找不到  也不懂怎么解决。
------其他解决方案--------------------
我刚试过这样的声明,没有任何问题啊……
------其他解决方案--------------------
#include <streambuf>
#include <stdio.h>
using namespace std;

void print_something(std::ostream &outstream)
{
  outstream << "helloworld, All of the output is going here\n";
}

int main(int argc, char **argv)
{
  if(argc > 1)
  {
    std::ofstream outfile(argv[1]);
    print_something(outfile);
  }
  else
  {
    print_something(std::cout);
  }
}


我试了上面这个例子  是通过的,为何在Trace的构造函数下会报错呢。
------其他解决方案--------------------
少了两头文件,呵呵:
#include <fstream>
#include <iostream>