C++ 关于c+中的流+;

C++ 关于c+中的流+;,c++,ostream,C++,Ostream,我想使用文件流和控制台输出流。在构造函数中,我希望根据传递给构造函数的参数,使用文件或控制台输出流进行初始化。然后我将在类中有另一个函数,它将输出重定向到该流。它的代码是什么?我正在尝试以下代码,这是不工作。 欢迎提出任何其他设计建议 class Test { private: std::ios *obj; std::ofstream file; std::ostream cout1; public: // Test()

我想使用文件流和控制台输出流。在构造函数中,我希望根据传递给构造函数的参数,使用文件或控制台输出流进行初始化。然后我将在类中有另一个函数,它将输出重定向到该流。它的代码是什么?我正在尝试以下代码,这是不工作。 欢迎提出任何其他设计建议

class Test
{
    private:

        std::ios *obj;
        std::ofstream file;
        std::ostream cout1;
    public:
//      Test(){}
        Test(char choice[])
        {
            if(choice=="file")
            {
                obj=new ofstream();
                obj->open("temp.txt");
            }
            else
                obj=new ostream();


        }
        void printarray()
        {
            for(int i=0;i<5;i++)

                     (*obj)<<"\n \n"<<"HI"
        }
}; 
类测试
{
私人:
标准:ios*obj;
流文件的std::of;
std::ostream cout1;
公众:
//Test(){}
测试(字符选择[])
{
如果(选项==“文件”)
{
obj=新的流();
obj->open(“temp.txt”);
}
其他的
obj=新的ostream();
}
void printary()
{

对于(int i=0;i这样的东西应该可以工作:

#include <iostream>
#include <fstream>
#include <string>

class Test
{
   private:

      std::ofstream file;
      std::ostream& obj;

   public:

      // Use overloaded constructors. When the default constructor is used,
      // use std::cout. When the constructor with string is used, use the argument
      // as the file to write to.
      Test() : obj(std::cout) {}
      Test(std::string const& f) : file(f.c_str()), obj(file) {}

      void printarray()
      {
         for(int i=0;i<5;i++)
            obj<<"\n " << "HI" << " \n";
      }
}; 

int main()
{
   Test a;
   a.printarray();

   Test b("out.txt");
   b.printarray();
}
#包括
#包括
#包括
课堂测试
{
私人:
流文件的std::of;
标准::奥斯特雷姆和obj;
公众:
//使用重载构造函数。使用默认构造函数时,
//使用std::cout。使用带字符串的构造函数时,请使用参数
//作为要写入的文件。
Test():obj(std::cout){}
Test(std::string const&f):文件(f.c_str()),obj(文件){}
void printary()
{

对于(int i=0;i这样的东西应该可以工作:

#include <iostream>
#include <fstream>
#include <string>

class Test
{
   private:

      std::ofstream file;
      std::ostream& obj;

   public:

      // Use overloaded constructors. When the default constructor is used,
      // use std::cout. When the constructor with string is used, use the argument
      // as the file to write to.
      Test() : obj(std::cout) {}
      Test(std::string const& f) : file(f.c_str()), obj(file) {}

      void printarray()
      {
         for(int i=0;i<5;i++)
            obj<<"\n " << "HI" << " \n";
      }
}; 

int main()
{
   Test a;
   a.printarray();

   Test b("out.txt");
   b.printarray();
}
#包括
#包括
#包括
课堂测试
{
私人:
流文件的std::of;
标准::奥斯特雷姆和obj;
公众:
//使用重载构造函数。使用默认构造函数时,
//使用std::cout。使用带字符串的构造函数时,请使用参数
//作为要写入的文件。
Test():obj(std::cout){}
Test(std::string const&f):文件(f.c_str()),obj(文件){}
void printary()
{

对于(int i=0;i这样的东西应该可以工作:

#include <iostream>
#include <fstream>
#include <string>

class Test
{
   private:

      std::ofstream file;
      std::ostream& obj;

   public:

      // Use overloaded constructors. When the default constructor is used,
      // use std::cout. When the constructor with string is used, use the argument
      // as the file to write to.
      Test() : obj(std::cout) {}
      Test(std::string const& f) : file(f.c_str()), obj(file) {}

      void printarray()
      {
         for(int i=0;i<5;i++)
            obj<<"\n " << "HI" << " \n";
      }
}; 

int main()
{
   Test a;
   a.printarray();

   Test b("out.txt");
   b.printarray();
}
#包括
#包括
#包括
课堂测试
{
私人:
流文件的std::of;
标准::奥斯特雷姆和obj;
公众:
//使用重载构造函数。使用默认构造函数时,
//使用std::cout。使用带字符串的构造函数时,请使用参数
//作为要写入的文件。
Test():obj(std::cout){}
Test(std::string const&f):文件(f.c_str()),obj(文件){}
void printary()
{

对于(int i=0;i这样的东西应该可以工作:

#include <iostream>
#include <fstream>
#include <string>

class Test
{
   private:

      std::ofstream file;
      std::ostream& obj;

   public:

      // Use overloaded constructors. When the default constructor is used,
      // use std::cout. When the constructor with string is used, use the argument
      // as the file to write to.
      Test() : obj(std::cout) {}
      Test(std::string const& f) : file(f.c_str()), obj(file) {}

      void printarray()
      {
         for(int i=0;i<5;i++)
            obj<<"\n " << "HI" << " \n";
      }
}; 

int main()
{
   Test a;
   a.printarray();

   Test b("out.txt");
   b.printarray();
}
#包括
#包括
#包括
课堂测试
{
私人:
流文件的std::of;
标准::奥斯特雷姆和obj;
公众:
//使用重载构造函数。使用默认构造函数时,
//使用std::cout。使用带字符串的构造函数时,请使用参数
//作为要写入的文件。
Test():obj(std::cout){}
Test(std::string const&f):文件(f.c_str()),obj(文件){}
void printary()
{
对于(int i=0;i
欢迎提出任何其他设计建议

class Test
{
    private:

        std::ios *obj;
        std::ofstream file;
        std::ostream cout1;
    public:
//      Test(){}
        Test(char choice[])
        {
            if(choice=="file")
            {
                obj=new ofstream();
                obj->open("temp.txt");
            }
            else
                obj=new ostream();


        }
        void printarray()
        {
            for(int i=0;i<5;i++)

                     (*obj)<<"\n \n"<<"HI"
        }
}; 
其中两个成员是无用的:

    std::ios *obj;
    std::ofstream file;
    std::ostream cout1;
你不能用
std::ios
做任何事情,一个与
stream
无关的
std::ostream
是无用的,而且你永远也不会使用
file
cout1

你想要:

    std::ofstream file;
    std::ostream& out;
如R Sahu的回答所示,并将其写入
out

    Test(char choice[])
    {
        if(choice=="file")
这不起作用,您需要使用
strcmp
来比较
char
字符串。您可能应该使用
std::string
而不是
char*

欢迎提出任何其他设计建议

class Test
{
    private:

        std::ios *obj;
        std::ofstream file;
        std::ostream cout1;
    public:
//      Test(){}
        Test(char choice[])
        {
            if(choice=="file")
            {
                obj=new ofstream();
                obj->open("temp.txt");
            }
            else
                obj=new ostream();


        }
        void printarray()
        {
            for(int i=0;i<5;i++)

                     (*obj)<<"\n \n"<<"HI"
        }
}; 
其中两个成员是无用的:

    std::ios *obj;
    std::ofstream file;
    std::ostream cout1;
你不能用
std::ios
做任何事情,一个与
stream
无关的
std::ostream
是无用的,而且你永远也不会使用
file
cout1

你想要:

    std::ofstream file;
    std::ostream& out;
如R Sahu的回答所示,并将其写入
out

    Test(char choice[])
    {
        if(choice=="file")
这不起作用,您需要使用
strcmp
来比较
char
字符串。您可能应该使用
std::string
而不是
char*

欢迎提出任何其他设计建议

class Test
{
    private:

        std::ios *obj;
        std::ofstream file;
        std::ostream cout1;
    public:
//      Test(){}
        Test(char choice[])
        {
            if(choice=="file")
            {
                obj=new ofstream();
                obj->open("temp.txt");
            }
            else
                obj=new ostream();


        }
        void printarray()
        {
            for(int i=0;i<5;i++)

                     (*obj)<<"\n \n"<<"HI"
        }
}; 
其中两个成员是无用的:

    std::ios *obj;
    std::ofstream file;
    std::ostream cout1;
你不能用
std::ios
做任何事情,一个与
stream
无关的
std::ostream
是无用的,而且你永远也不会使用
file
cout1

你想要:

    std::ofstream file;
    std::ostream& out;
如R Sahu的回答所示,并将其写入
out

    Test(char choice[])
    {
        if(choice=="file")
这不起作用,您需要使用
strcmp
来比较
char
字符串。您可能应该使用
std::string
而不是
char*

欢迎提出任何其他设计建议

class Test
{
    private:

        std::ios *obj;
        std::ofstream file;
        std::ostream cout1;
    public:
//      Test(){}
        Test(char choice[])
        {
            if(choice=="file")
            {
                obj=new ofstream();
                obj->open("temp.txt");
            }
            else
                obj=new ostream();


        }
        void printarray()
        {
            for(int i=0;i<5;i++)

                     (*obj)<<"\n \n"<<"HI"
        }
}; 
其中两个成员是无用的:

    std::ios *obj;
    std::ofstream file;
    std::ostream cout1;
你不能用
std::ios
做任何事情,一个与
stream
无关的
std::ostream
是无用的,而且你永远也不会使用
file
cout1

你想要:

    std::ofstream file;
    std::ostream& out;
如R Sahu的回答所示,并将其写入
out

    Test(char choice[])
    {
        if(choice=="file")


这不起作用,您需要使用
strcmp
来比较
char
字符串。您可能应该使用
std::string
而不是
char*

为什么不在流中传递一个
std::ostream&
成员呢?这听起来像是在问您发明的解决方案Y到真正的问题X。真正的pr是什么问题X?@Cheersandhth.-Alf我正在试图解决我所提到的代码所解释的问题,但该代码不起作用。@chris能否请您提供您所说的代码。@chris的意思是让调用者将
std::ofstream
std::cout
作为单个构造参数和ini传递从该参数初始化(并稍后使用)一个
std::ostream&
。调用方拥有传入的文件流(如果这是所采用的路由),否则它只传入
std::cout
为什么不在传入流的位置只传入一个
std::ostream&
成员?这听起来像是在询问您的in