Search code examples
c++c++11ostream

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


the error makes no sense to me, I've done stuff like this before and followed it to a tee but now it just pops up.

#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

this is the error: error: non-member function 'std::ostream& operator<<(std::ostream&, const Matrica&)' cannot have cv-qualifier|


Solution

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

    You declare ostream& operator << (ostream&, const Matrica&) as friend, which makes it a free function, not a member function. Free functions do not have the this pointer, which is being affected by const modifier at the end of function declarations. It means that if you have a free function, you cannot make it a const function, since there is no this pointer to be affected by it. Simply delete it like so:

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

    And you are ready to go.