我有这个错误
vect.hpp:13:33: error: declaration of ‘operator<<’ as non-function
对于代码:
#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<<
的实例化,但没有主模板声明。您需要提前声明运算符(operator)模板。例如
// 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> &);
};
LIVE
关于c++ - vect.hpp:13:33:错误: ‘operator<<’声明为无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61672608/