试图写std:out和文件在同一时间 [英] trying to write std:out and file at the same time

查看:116
本文介绍了试图写std:out和文件在同一时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过重载cstream在c ++中同时写入文件和stdout



test.h

  #pragma once 

#include< iostream>

使用std :: ofstream;

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


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

test.cpp

  OutputAndConsole文件(output.txt); 
file<< test;

文件中的输出是

  01400930 

,但在控制台中

 测试

它看起来像进入

  _Myt& __CLR_OR_THIS_CALL operator< <(const void * _Val)

我做错了什么?

解决方案

问题




  ofstream :: operator<< (var); 


这是您使用 ofstream :: < 作为限定的函数调用。你要求函数查找找到 ofstream 的成员函数;成员的最佳匹配是 void * 的一个,而 char * 的打印实际字符串内容是一个自由函数(即不是成员函数)。



如果你这样做, $ c> cout :

  std :: cout.operator< ; 



解决方案



  static_cast< ofstream&>(* this)< var; 

,因为您仍然使用正常的运算符语法(具有所有的重载分辨率)以 ofstream 作为LHS操作数。



虽然我没有实际测试过。 p>

结论



另外, 运算符< ;



因此:

  struct OutputAndConsole:std :: ofstream 
{
OutputAndConsole(const std :: string& fileName)
:std :: ofstream(fileName)
,fileName(fileName)
{};

const std :: string fileName;
};

template< typename T>
OutputAndConsole& operator<<<(OutputAndConsole& strm,const T& var)
{
std :: cout< var;
static_cast< std :: ofstream&>(strm)<< var;
return strm;
};

我也自由地做一些小的语法调整。


I am trying to write to file and stdout at the same time within c++ by overloading ofstream

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);
};

test.cpp

  OutputAndConsole file("output.txt");
  file << "test" ;

The output in the file is

01400930

but in the console is

test

I debugged the code it looks like it is entering into

_Myt& __CLR_OR_THIS_CALL operator<<(const void *_Val)

What am I doing wrong?

解决方案

The problem

ofstream::operator << (var);

It's your use of ofstream::operator<< as a qualified function call. You're mandating that the function lookup locate a member function of ofstream; the best match that's a member is the one for void*, whereas the specialisation for char* that prints the actual string contents is a free function (i.e. not a member function).

You'll find the same problem if you do this with cout, too:

std::cout.operator<<(var);

The solution

This might do it:

static_cast<ofstream&>(*this) << var;

because you're still using normal operator syntax (with all the overload resolution that this entails), but doing so with an ofstream as the LHS operand.

I haven't actually tested it, though.

Conclusion

As an aside, your operator<< ought to be a free function too, in order to fit in with this convention.

So:

struct OutputAndConsole : std::ofstream
{
    OutputAndConsole(const std::string& fileName)
       : std::ofstream(fileName)
       , fileName(fileName)
    {};

    const std::string fileName;
};

template <typename T>
OutputAndConsole& operator<<(OutputAndConsole& strm, const T& var)
{
    std::cout << var;
    static_cast<std::ofstream&>(strm) << var;
    return strm;
};

I also took the liberty of making some minor syntax adjustments.

这篇关于试图写std:out和文件在同一时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆