14

cout が ostream クラスのオブジェクトである場合、同じクラスから「out」など、独自のオブジェクトを宣言できないのはなぜですか。つまり、次のコードは機能するはずではありませんか??

#include<iostream>
using namespace std;
int main()
{
    ostream out;
    out<<"something";
}

もしくはそうでないか

#include<iostream>
using namespace std;
int main()
{
    ostream_withassign out;
    out<<"something";
}
4

4 に答える 4

1

ostream オブジェクト (このストリームの出力先) を設定していないため、もちろん使用できません。 http://www.cplusplus.com/reference/iostream/ostream/ostream/
すなわち

// ostream constructor
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  filebuf fb;
  fb.open ("test.txt",ios::out);
  ostream os(&fb);
  os << "Test sentence\n";
  fb.close();
  return 0;
}
于 2012-04-09T10:16:32.597 に答える