相关疑难解决方法(0)

试图写std:out并同时归档

我试图通过重载流来在c ++中同时写入文件和标准输出

test.h

 #pragma once 

#include <iostream>

using  std::ofstream;

class OutputAndConsole:public ofstream
{
public:
    std::string fileName;        
    OutputAndConsole(const std::string& fileName):ofstream(fileName),fileName(fileName){
    };
    template <typename T>
    OutputAndConsole& operator<<(T var);
};


template <typename T>
OutputAndConsole& OutputAndConsole::operator<<(T var)
{
    std::cout << var;
    ofstream::operator << (var);
    return (*this);
};
Run Code Online (Sandbox Code Playgroud)

TEST.CPP

  OutputAndConsole file("output.txt");
  file << "test" ;
Run Code Online (Sandbox Code Playgroud)

文件中的输出是

01400930
Run Code Online (Sandbox Code Playgroud)

但在控制台是

test
Run Code Online (Sandbox Code Playgroud)

我调试了它看起来正在进入的代码

_Myt& __CLR_OR_THIS_CALL operator<<(const void *_Val)
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

c++ iostream

3
推荐指数
2
解决办法
4207
查看次数

标签 统计

c++ ×1

iostream ×1