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

时间:2012-12-31 08:31:56

标签: c++ visual-studio-2010 templates operator-overloading friend-function

我遇到了重载运算符<<用于模板类。我使用的是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

主要是我有

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

其中Integer只是int的包装器,具有我需要的一些特殊功能。

然而,当我编译上面的代码时,我收到错误C2679,其中显示binary '<<' : no operator found which takes a right-hand operand of type 'Polyff::FiniteField<T,n>' (or there is no acceptable conversion)

我还尝试删除友元声明中的参数,因此代码变为:

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

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

所以我想知道如何更改代码以便编译以及为什么? 谢谢!

-------------------------编辑于2012.12.31 ----------------- ----------

代码现在用g ++编译。 Here是github存储库。

3 个答案:

答案 0 :(得分:1)

这似乎按预期工作:

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

我刚刚在模板参数中用对象的指针替换了引用,因为指向全局对象(静态或非静态)的指针是在编译时(或至少链接时)已知的信息。我不知道接受引用的语言。

请注意,在此示例中,将打印0,因为它对应于_val的默认构造。

答案 1 :(得分:0)

我尝试在Visual C ++ 2010上编译代码。我得到了和你一样的错误。

在你的代码中,Visual实际上有两件事情不喜欢:

  1. N不是编译时常量(这是对的,没有?)。
  2. 然后问题是参考编译时常量。因此,您应该在所有模板参数中将“T&amp; n”替换为“T n”。
  3. 那为我做了这份工作!

答案 2 :(得分:0)

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

相关问题