I'm working on extraction and insertion overloads. And I can't access the private variables while using friend.
This is my code based a standard tutorials and what my teacher gave me to work with
Main.cpp
#include <iostream>
#include "Obj.h"
using namespace std;
using namespace N;
Obj a;
int main() {
cout << a;
}
Obj.cpp
#include "Obj.h"
#include <iostream>
using namespace std;
using namespace N;
ostream &operator<<(ostream &output, const Obj &a) {
output << a.h;
return output;
}
Obj.h
#ifndef OBJ_H
#define OBJ_H
#include <iostream>
using namespace std;
namespace N {
class Obj {
private:
string h;
public:
//constructors
Obj() {
h = "default";
}
//overloads
friend ostream &operator<<(ostream &output, const Obj &a);
};
}
#endif
I started from a header file setup and broke it down to this while getting the same error code. In particular I keep getting this result.
Obj.cpp:9:15: error: 'h' is a private member of 'N::Obj'
output << a.h;
^
./Obj.h:12:14: note: declared private here
string h;