当我编译引用std :: ostream

时间:2017-11-14 15:04:49

标签: c++ c++11 ostream

这个错误对我来说毫无意义,我之前已经完成了这样的事情并且跟着它发球,但是现在它刚刚弹出。

#ifndef MATRICA_HPP_INCLUDED
#define MATRICA_HPP_INCLUDED

#include <iostream>
#include <cstdio>

#define MAX_POLJA 6
using namespace std;

class Matrica {
private:
short int **mtr;
short int **ivicaX;
short int **ivicaY;
int lenX, lenY;
public:
Matrica(short int, short int);
~Matrica();

int initiate_edge(const char *, const char *);
short int get_vrednost (short int, short int) const;
short int operator = (const short int);
int check_if_fit(int *);

friend ostream& operator << (ostream&, const Matrica&) const; // HAPPENS HERE <====
};

#endif // MATRICA_HPP_INCLUDED

这是错误: error: non-member function 'std::ostream& operator<<(std::ostream&, const Matrica&)' cannot have cv-qualifier|

1 个答案:

答案 0 :(得分:6)

friend ostream& operator << (ostream&, const Matrica&) const;

您将ostream& operator << (ostream&, const Matrica&)声明为friend,这使其成为免费功能,而不是成员函数。自由函数没有this指针,它在函数声明结束时受const修饰符的影响。这意味着如果你有一个自由函数,你不能使它成为const函数,因为没有this指针受其影响。只需将其删除即可:

friend ostream& operator << (ostream&, const Matrica&);

你准备好了。