简体   繁体   English

重载运算符<<用于模板类

[英]Overload operator<< for template class

I am having problem with overloading operator<< for a template class. 我有重载operator <<为模板类的问题。 I am using Visual Studio 2010, and here is my code. 我使用的是Visual Studio 2010,这是我的代码。

#ifndef _FINITEFIELD
#define _FINITEFIELD
#include<iostream>

namespace Polyff{
    template <class T, T& n> class FiniteField;
    template <class T, T& n> std::ostream& operator<< (std::ostream&, const FiniteField<T,n>&);

    template <class T, T& n> class FiniteField {
    public:
            //some other functions
    private:
        friend std::ostream& operator<< <T,n>(std::ostream& out, const FiniteField<T,n>& obj);
        T _val;
    };

    template <class T, T& n>
    std::ostream& operator<< (std::ostream& out, const FiniteField<T,n>& f) {
        return  out<<f._val;
    }
    //some other definitions
}
#endif

In main I just have 在主要我只是

#include"FiniteField.h"
#include"Integer.h"
#include<iostream>
using std::cout;
using namespace Polyff;
Integer N(5);

int main () {

    FiniteField<Integer, N> f1;
    cout<< f1;  
}

where Integer is just a wrapper of int with some special functionality I need. 其中Integer只是int的包装器,具有我需要的一些特殊功能。

However, when I compile the above code, I got error C2679, which says binary '<<' : no operator found which takes a right-hand operand of type 'Polyff::FiniteField<T,n>' (or there is no acceptable conversion) 但是,当我编译上面的代码时,我得到了错误C2679,它表示binary '<<' : no operator found which takes a right-hand operand of type 'Polyff::FiniteField<T,n>' (or there is no acceptable conversion)

I have also tried to remove the parameters in the friend declaration so the code becomes: 我还尝试删除友元声明中的参数,以便代码变为:

friend std::ostream& operator<< <> (std::ostream& out, const FiniteField<T,n>& obj);

But this produce another error: C2785: 'std::ostream &Polyff::operator <<(std::ostream &,const Polyff::FiniteField<T,n> &)' and '<Unknown>' have different return types 但这会产生另一个错误:C2785: 'std::ostream &Polyff::operator <<(std::ostream &,const Polyff::FiniteField<T,n> &)' and '<Unknown>' have different return types

so I am wondering how should I change the code so it compiles and why? 所以我想知道我应该如何更改代码以便编译以及为什么? Thanks! 谢谢!

------------------------- edited on 2012.12.31 --------------------------- -------------------------编辑于2012.12.31 -------------------- -------

The code compiles with g++ now. 代码现在用g ++编译。 Here is the github repository. 是github存储库。

This seem to work as expected: 这似乎按预期工作:

namespace Polyff{
  template <class T, T* n> class FiniteField;
  template <class T, T* n> std::ostream& operator<< (std::ostream&, const FiniteField<T,n>&);

  template <class T, T* n> class FiniteField {
  public:
    //some other functions
  private:
    friend std::ostream& operator<< <T,n>(std::ostream& out, const FiniteField<T,n>& obj);
    T _val;
  };

  template <class T, T* n>
  std::ostream& operator<< (std::ostream& out, const FiniteField<T,n>& f) {
    return  out << f._val.n; // I added the field of my Integer class
  }
  //some other definitions
}


struct Integer{
  Integer() : n(0){}
  Integer(int nn) : n(nn){}
  int n;
};

using std::cout;
using namespace Polyff;
Integer N(5);

int main () {
  FiniteField<Integer, &N> f1;
  cout<< f1;  
}

I just replaced the reference by the pointer of your object in the template arguments, since a pointer to a global object (static or not) is an information known at compile-time (or at least link time). 我刚刚在模板参数中用对象的指针替换了引用,因为指向全局对象(静态或非静态)的指针是在编译时(或至少链接时)已知的信息。 I am not aware of the language accepting references. 我不知道接受引用的语言。

Note that in this example, 0 will be printed because it corresponds to the default construction of _val . 请注意,在此示例中,将打印0因为它对应于_val的默认构造。

I tried to compile your code on my Visual C++ 2010. I got the same error as you did. 我试图在我的Visual C ++ 2010上编译你的代码。我得到了和你一样的错误。

There are actually two things that Visual did not like in your code: 在你的代码中,Visual实际上有两件事情不喜欢:

  1. N is not a compile time constant (which is right, no?). N不是编译时常量(这是对的,没有?)。
  2. The problem is then to take the reference of a compile time constant. 然后问题是参考编译时常量。 So, you should replace "T& n" by "T n" in all your template arguments. 因此,您应该在所有模板参数中将“T&n”替换为“T n”。

That made the job for me! 这使我的工作!

您应该使用普通变量( T n而不是T& n )替换引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM