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

I have a weird error that pops up when I compile referring to std::ostream

本文关键字:错误 有一个 引用 编译 std ostream      更新时间:2023-10-16

这个错误对我来说毫无意义,我以前做过这样的事情,并跟随它到发球台,但现在它只是弹出。

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

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

您将ostream& operator << (ostream&, const Matrica&)声明为 friend ,这使它成为一个自由函数,而不是一个成员函数。自由函数没有 this 指针,它受到函数声明末尾const修饰符的影响。这意味着如果你有一个自由函数,你不能让它成为一个const函数,因为没有this指针受它的影响。只需像这样删除它:

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

你准备好了。

相关文章: