当我编译引用std :: ostream时,我弹出一个奇怪的错误

Fis*_*ish 2 c++ ostream c++11

这个错误对我没有任何意义,我之前已经做过这样的事情并且跟着它发球但现在它只是弹出.

#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
Run Code Online (Sandbox Code Playgroud)

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

Fur*_*ish 6

friend ostream& operator << (ostream&, const Matrica&) const;
Run Code Online (Sandbox Code Playgroud)

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

friend ostream& operator << (ostream&, const Matrica&);
Run Code Online (Sandbox Code Playgroud)

你准备好了.