c++ - 当我引用 std::ostream 进行编译时,会弹出一个奇怪的错误

标签 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|

最佳答案

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

你声明ostream& operator << (ostream&, const Matrica&)作为friend ,这使它成为一个自由函数,而不是一个成员函数。免费功能没有 thisconst 影响的指针函数声明末尾的修饰符。这意味着如果你有一个免费的功能,你不能把它变成 const功能,因为没有this受其影响的指针。像这样简单地删除它:

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

你准备好了。

关于c++ - 当我引用 std::ostream 进行编译时,会弹出一个奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47288998/

相关文章:

c++ - 清除 QGraphicsItem::paint 生成的 "unused parameter ' 小部件'"警告的清洁方法

java - 使用 CreateProcess 从 Win32 C++ 应用程序启动 Java 应用程序时出错

c++ - 为什么没有分段元组构造?

c++ - boost:bind 和 io_service 在两个不同的类中

c++ - 在此流运算符重载中由 std::ostream::sentry 引起的段错误

c++ - 在模板类 C++ 中使用函数指针

c++ - QMainWindow 不使用 setMouseTracking() 跟踪鼠标

c++ - 禁用 GCC 自动并行化

c++ - 我可以设置 ostream 分隔符吗

c++ - cout 能抛出异常吗?