【问题标题】:OutputStream Prints '1', For Some Reason出于某种原因,OutputStream 打印“1”
【发布时间】:2014-02-22 18:07:07
【问题描述】:

由于某种原因,当我尝试将 std::endl 与我的 OutputStream 对象一起使用时,它会在屏幕上和文件中打印“1”!! (我认为它实际上是在尝试打印 OutputStream 对象本身,但我可能是错的。)这是我的代码:

输出流.h

#ifndef OUTPUTSTREAM_H
#define OUTPUTSTREAM_H

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

class OutputStream : public std::ostream
{
    public:
        OutputStream(const std::string&);
        virtual ~OutputStream();
        //template <typename T> OutputStream& operator<<(T);
        template <typename T> OutputStream& operator<<(T& data)
        {
            std::cout << data;
            *(this->file) << data;
            return *this;
        }
        template <typename T> OutputStream& operator<<(const T& data)
        {
            std::cout << data;
            *(this->file) << data;
            return *this;
        }
        void changeDestinationTo(const std::string&);
        std::string getDestination() const;
        // overloading the endl operator to allow for the same functionality that exists on other ostream objects, and returning
        //  the same type allows for cascading calls
        static OutputStream& endl(OutputStream&);
    protected:
    private:
        std::string filename;
        std::ofstream * file;
};

#endif // OUTPUTSTREAM_H

输出流.cpp

#include "OutputStream.h"

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

OutputStream::OutputStream(const std::string& theFileName)
{
    // specify the filename
    this->filename = theFileName;
    // open the fileName with that file
    this->file = new(std::nothrow) std::ofstream(this->filename.c_str());
}

OutputStream::~OutputStream()
{
    // delete the file! (no, not really)
    this->file->close();
    delete this->file;
}

/*template <typename T>
OutputStream& OutputStream::operator<<(T)
{
    std::cout << data;
    *(this->file) << data;
    return *this;
}

template <typename T>
OutputStream& OutputStream::operator<<(T& data)
{
    std::cout << data;
    *(this->file) << data;
    return *this;
}

// the const-correct version of the above function
template <typename T>
OutputStream& OutputStream::operator<<(const T& data)
{
    // writing the data to std::cout
    std::cout << data;
    // writing the data to the file that we specify
    *(this->file) << data;
    return *this;
}
*/
void OutputStream::changeDestinationTo(const std::string& newFileName)
{
    // close the currently-open file
    this->file->close();
    // open up the file at newFileName
    this->filename = newFileName;
    this->file->open(newFileName.c_str());
}

std::string OutputStream::getDestination() const { return this->filename; }

OutputStream& OutputStream::endl(OutputStream& myStream)
{
    // call std::endl on both std::cout and *(this->file)
    std::cout << std::endl;
    *(myStream.file) << std::endl;
    // allow for cascading by returning myStream
    return myStream;
}

main.cpp

#include "Array.h"
#include "OutputStream.h"

#include <iostream>

using namespace std;

int main()
{
    // setup the OutputStream
    OutputStream outputter("file.txt");
    // do some stuff with it
    outputter << "This is a test to make sure that it works.\n";
    outputter << "25 + 3.7 == " << 25 + 3.7 << '\n'; // doesn't print '1'
    outputter << "\nNow testing this with objects:\n" << OutputStream::endl; // prints '1'
    // declare an Array of 10 ints
    Array<int> someArray(10);
    // output them
    outputter << "Printing an Array:\n";
    outputter << someArray << OutputStream::endl;
    return 0;
}

有什么方法可以轻松解决这个问题吗? (我尝试过声明 friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; standardStream, const OutputStream&amp; myOutputStream) { return standardStream; } 但这并没有解决问题......

【问题讨论】:

    标签: c++ operator-overloading ostream


    【解决方案1】:

    我相信您需要像这样为函数指针(操纵器)创建重载:

    template<typename T>
    OutputStream& operator<<(std::ostream& (*manip)(std::ostream&))
    {
        manip(*this->file);
        manip(std::cout);
    
        return *this;
    }
    

    【讨论】:

    • 是的,这就是我最终所做的,而且它奏效了!我在这里找到了该解决方案:stackoverflow.com/questions/1134388/… 尽管如此,我仍然会尝试理解为什么我的方式会产生结果(我原来的方式仍然有效,但每次我使用 endl 时都会打印出“1”)。我想我知道为什么,我会试着在这里用文字表达; // 可能需要一段时间
    • @MikeWarren 原因是因为您正在打印一个选择 ostream::operator&lt;&lt;(ostream&amp;, bool) 的函数指针,因此它被隐式转换为 bool,因此输出为 1。
    • 天哪,我刚刚在看到这个之前就得出了这个结论(并且只是想着发生了什么)! (就是我想了想,贴出答案,刷新页面的时候看到你的评论!!真是巧合!!) !
    【解决方案2】:

    我试图将函数指针 (endl) 传递给 operator

    【讨论】:

      【解决方案3】:

      尝试重载operator&lt;&lt; 以接受指向方法的指针作为参数。

      第二种选择是在 OutputStream 中创建一个名为(例如)endl 的新类(类似于定义类特定异常的方式),如下所示:

      class endl{}
      

      之后你需要做的就是让 operator

       OutputStream& operator<<(OutputStream::endl& endl){
          cout << endl;
          *flie << endl;
          return *this;
          }
      

      并像这样使用它:

      outputer << "some string" << endl();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-07-29
        • 2021-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多