【问题标题】:c++ manipulatorc++ 操纵器
【发布时间】:2010-05-26 05:48:37
【问题描述】:

我有一门课:

template<class T>
class matrix
{
private:
    int COLS,ROWS;
public:
    inline matrix(int r,int c){
        this->COLS=r;
        this->ROWS=c;
    }

    template<class T2>
    friend ostream& info(ostream& os);
};

我尝试了很多方法来实现信息功能。但没有一个成功。

我想在主函数中使用它

Matrix<int> M(10,20);

cout<<info<<M;

我想输出 Matrix 类的列和行信息。

我在实现朋友类信息方面尝试了很多次,但都失败了。

谁能告诉我怎么做?

对不起,我忘了放

template<class T2>
friend ostream& operator<< (ostream &out,matrix<T2> &cMatrix);

实施:

template<class T2>
ostream & operator<<(ostream &out, matrix<T2> &cMatrix) {
    out<<cMatrix.getCOLS();// sorry for didn't put the get function, it's not easy to put code line by line here.
    out<<cMatrix.getROWS();
    return out;
}

我的

但是当我想使用信息时,我得到了错误。

我不确定,如何将自己的类型操纵器实现为友元函数。 我谷歌了一些,但它们不是朋友功能。而且,它是一种模板 功能。

这就是我想要的:

template<class T2>
ostream& info(ostream& os,matrix<T2> &cMatrix)
{
    int cols=cMatrix.getCOLS();
    int rows=cMatrix.getROWS();
    os<<rols<<"X"<<rows<<"  matrix "<<endl;
    return os;
}

【问题讨论】:

  • 别忘了正确缩进你的源代码。
  • 为什么需要使用机械手?免费或成员函数会容易得多。另一种选择是使用proxy class
  • 主持人,我正在努力完成我的一项任务。老师让我们练习机械手的东西。所以,他让我把这个作为必要的功能来实现。

标签: c++ formatting iostream


【解决方案1】:

如果你想模拟像cout &lt;&lt; setw(8) &lt;&lt; … 这样的操纵器,没有标准的工具,而且不能只用一个函数来完成。

接受参数的操纵器是创建特殊函子的工厂函数。返回的对象将参数记住给操纵器并实现operator&lt;&lt;以使用它。

示例代码中有两种不同的方法。

cout<<info<<M; // info changes cout somehow?

ostream& info(ostream& os,matrix<T2> &cMatrix) // info behaves like operator<<?

第一种方法,将状态添加到cout,是可行的,但太复杂了。

第二种方法引出了一个问题,为什么你不直接称它为operator&lt;&lt; 并完成。

或者,您可以直接拨打第二个电话:info(cout &lt;&lt; "hello", M) &lt;&lt; "world";

如果您正在寻找语法cout &lt;&lt; info(M),它更像是一个操纵器,您需要类似的东西

struct print_info {
    Matrix &m;
    print_info( Matrix &in ) : m(in) {}
    friend ostream &operator<<( ostream &str, print_info const &pi ) {
         str << pi.m.getcols();
         …
    }
};

print_info info( Matrix &in )
    { return print_info( in ); }

严格来说,函数是不必要的,但最好不要给具有这样一个特定函数的类起一个通用名称,如info

但话又说回来,所有这些都是不必要的。只需使用您拥有的 &lt;&lt; 覆盖即可。

【讨论】:

    【解决方案2】:

    如果您不需要粘性,一种可能的方法:

    class InfoPrinter {
        std::ostream& os_;
    public:
        InfoPrinter(std::ostream& os) : os_(os) {}
        template<class T> std::ostream& operator<<(const Matrix<T>& m) {
            return os_ << m.getCols() << ", " << m.getRows();
        }
    };
    
    struct Info {} info;
    
    InfoPrinter operator<<(std::ostream& os, const Info&) {
        return InfoPrinter(os);
    }
    

    用法例如:

    std::cout << info << Matrix<int>(2,3);
    

    【讨论】:

      【解决方案3】:

      我相信只需将ostream&amp; operator&lt;&lt;(ostream&amp; os); 创建为 Matrix 类的成员即可。那你可以去cout &lt;&lt; M &lt;&lt; endl;

      【讨论】:

      • 您不希望 operator&lt;&lt; 成为 Matrix 类的成员,因为调用序列将是 M &lt;&lt; cout
      【解决方案4】:

      您应该尝试重载输出运算符。单击here 以获取有关如何操作的信息。如果您发布更新后的代码,我会在必要时为您提供更多帮助。

      【讨论】:

        猜你喜欢
        • 2018-07-07
        • 2010-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多