I am trying to implement Heap ADT in C . I am currently facing a problem in the my overloaded operator<<. I saw many solutions but non worked for me: first solution || Here is the second one
Here is the hpp file:
#ifndef Heap_hpp
#define Heap_hpp
#include <stdio.h>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
typedef int elementType;
class Heap{
private:
vector<elementType> myVecrtor;
int mySize = 1; //The minimum size is 1 since
//the first element is a dummy.
public:
Heap();
//some functions...
//My problem is in this function
friend ostream& operator<<(ostream &out, Heap &h);
void perculateDown(int root);
void perculateUp();
};
#endif /* Heap_hpp */
Here is the cpp file:
#include "Heap.hpp"
using namespace std;
Heap::Heap(){
}
ostream& Heap::operator<<(ostream &out, Heap &h){//it is giving me the error here
out<<"\t\tHeap:";
for(int i = 0; i < mySize; i ){
out<<myVecrtor.at(i);//this is not what i actualy want to display, just a demo
}
return out;
}
I need to mention that i tried it not as a friend and it gave me the same error And when initializing the function inside the cpp file like this:
ostream& operator<<(ostream &out, Heap &h)
the error was gone but i wasn't able to use any member. Any help other than the solutions I have mentioned would be appreciated.
Also removing the qualification (Heap::) prevents me from using any member of my class for some reason. error: Use of undeclared identifier 'mySize'
image showing the error I tried using h.mySize, it gave me: 'mySize' is a private member of 'Heap' private member error photo
CodePudding user response:
The problem is that you've defined the overloaded operator<<
as a member function of class Heap
instead of defining it as a non-member function.
To solve this you should remove the Heap
qualification(Heap::
) while defining the overloaded operator<<
as shown below:
//------v-----------------------------------> removed Heap:: from here
std::ostream& operator<<(std::ostream &out, Heap &h){
for(int i = 0; i < h.mySize; i ){
//---------------------^^-------------------->added h. here
out<<h.myVecrtor.at(i);
//-----------^^------------------------------>added h. here
}
return out;
}
Additionally, when implementing the overloaded operator<<
, we make the second parameter as an lvalue reference to const
since operator<<
doesn't change the state of the object. So after modification the code would look something like:
class Heap{
//other code as before
public:
//------------------------------------------vvvvv------------>added const here
friend std::ostream& operator<<(std::ostream &out,const Heap &h);
};
//------v---------------------------------------------> removed Heap:: from here
std::ostream& operator<<(std::ostream &out,const Heap &h){
//-------------------------------^^^^^---------------->added const here
for(int i = 0; i < h.mySize; i ){
out<<h.myVecrtor.at(i);//this is not what i actualy want to display, just a demo
}
return out;
}
Important Node
Note that you should not use using namespace std;
inside header files like you've done in your example. You can refer to Why is "using namespace std;" considered bad practice?.
Also, note that since operator<<
is a non-member function, so we have to use h.mySize
and h.myVecrtor.at(i)
instead of just using mySize
and myVecrtor.at(i)
.