当前位置: 首页 > 知识库问答 >
问题:

模板类的重载运算符

颜志学
2023-03-14

我在运算符过载时遇到问题

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

其中<code>Integer</code>只是<code>int</code>的包装,带有我需要的一些特殊功能。

然而,当我编译上面的代码时,我得到了错误C2679,它表示<code>binary'

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

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

但这会产生另一个错误:C2785:的std::ostream

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

-------------------------2012年12月31日编辑---------------------------

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


共有3个答案

幸弘扬
2023-03-14

您只需将引用替换为普通变量(tn),而不是T

柯振濂
2023-03-14

我试着在我的Visual C 2010上编译你的代码。我和你犯了同样的错误。

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

    < li>N不是编译时常数(哪个是正确的,不是?). < li >接下来的问题是引用编译时间常数。因此,您应该替换“T

这就是我的工作!

洪逸清
2023-03-14
匿名用户

这似乎如预期那样工作:

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

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

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

 类似资料:
  • 我在surf.h中有以下代码,其中声明了一个具有两种不同类型的模板类: 然后定义一个新类,其中创建了一个类型为T的向量(在field.h中) 在main.cpp中,我使用了这个字段。 我用G++(版本5.4)编译它,得到以下错误: 从main.cpp:2:0:field.h:在“std::ostream&operator<<(std::ostream&,const field&)[with T=s

  • 我有一个模板类包含其他类的优先级队列,我需要使用优先级重载器调用各个类重载器,根据各个类的偏好进行比较(在这种情况下是年龄,在另一个类中可能是价格。 我绝对相信我已经实现了不正确的运算符重载,因此非常感谢您的建议。 举个例子 我得到这个错误,我不知道我需要做什么来修复它。我必须将类重载保持为单个变量(Animal) 任务cpp:在“布尔运算符”的实例化中

  • 我对这个错误有意见 错误LNK2019未解析的外部符号"类std::basic_ostream 现在,< code>post所做的就是调用< code >操作符 宣言 定义 它们分别位于文件和中,我要求运算符不是成员函数(用于赋值)。

  • 我正在为分数制作一个模板类,分子和分母可以是int、浮点数或双精度类型。当重载算术或关系运算符时,当我尝试添加两个类时,它会出现错误,比如A类和B类 在头文件“fraction. h”中 在cpp文件中 我期望它显示10.1/12.1,但是它给出编译器错误C2678,说二进制“”:没有找到接受“fraction”类型的左操作数的运算符(或者没有可接受的转换)。我花了很多时间试图解决这个问题,现在我

  • 在下面的代码中,是一个模板类,取决于非类型参数。为和定义了friend。还依赖于另一个bool模板参数。 在Coliru上看现场直播。 现在,我想给出的模板参数的默认值,例如,以便以下语句 相当于 如果我在 同样,它不编译给出错误 main.cpp:27:15:错误:重新声明friend'template std::ostream&operator<<(std::ostream&,const a&

  • 简短的问题:运营商是否有特殊的模板查找规则,用于通过内部链接进行重载解决,或者底部的代码是否是GCC中操作员的模板重载解决错误? 细节:我不会粘贴一大块代码,而是带你完成我的推理。让我们从一些简单的代码开始: 上面印着,一切都很好。 现在,让我们将两个 放在一个匿名命名空间中: 代码现在无法编译。Clang表示和GCC。 这是坏的