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