本文介绍了vect.hpp:13:33:错误:将"operator<<"声明为无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到此错误

获取代码:

#include <iostream>

template<unsigned d>
class Vect{
    protected:
        double * coord;
    public:
        Vect() {for(int i=0, i<d, i++){*(coord+i)=0;}}
        ~Vect(){delete coord; coord=nullptr;}
        Vect(const Vect &);
        double operator=(const Vect &);
        double operator[](unsigned i) const{return *(coord+i);}
        friend std::ostream & operator<< <>(std::ostream &, const Vect<d> &);
};

该行:

friend std::ostream & operator<< <>(std::ostream &, const Vect<d> &);

推荐答案

friend声明是指 operator<< 的实例化,但是没有主模板声明.您需要事先声明操作员模板.例如

The friend declaration refers to the instantiation of the operator<< but there's no primary template declaration. You need to declare the operator template in advance. e.g.

// forward declaration
template<unsigned d>
class Vect;

// primary template declaration of operator<<
template<unsigned d>
std::ostream & operator<< (std::ostream &, const Vect<d> &);

template<unsigned d>
class Vect{
    protected:
        double * coord;
    public:
        Vect() {for(int i=0; i<d; i++){*(coord+i)=0;}}
        ~Vect(){delete coord; coord=nullptr;}
        Vect(const Vect &);
        double operator=(const Vect &);
        double operator[](unsigned i) const{return *(coord+i);}
        friend std::ostream & operator<< <>(std::ostream &, const Vect<d> &);
};

实时

这篇关于vect.hpp:13:33:错误:将"operator&lt;&lt;"声明为无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 18:54