When trying to run, the compiler tells me that I cannot access the private data members. I'm just curious as to what's causing this error. Here's my class declaration:

#include <iostream>
#include <string>

using namespace std;

#ifndef SALARY_H
#define SALARY_H

class Salary
{
	public: const int getDepNum();
			const int getWorkerID();
			const double getYearlySalary();
			const string getDepartment();
			void setDepNum(int x);
			void setWorkerID(int x);
			void setYearlySalary(double x);
			void setDepartment(string x, Salary salary);
			const double newSalary();
			const double monthlySalary();
			const double weeklySalary();

	private: int depNum, workerID;
			 double yearlySalary;
			 string department;

			friend	ostream& operator <<(ostream &out, Salary &x);
			friend	istream& operator >>(ifstream &in, Salary &x);
};

#endif

and the definition:

istream& operator >> (istream &in, Salary &x)
{
	
	in >> x.depNum >> x.workerID >> x.yearlySalary;

	return in;
}

Recommended Answers

All 5 Replies

how are you trying to use Salary::operator>> ? What exactly is the error?

You have declared operator>> as private , so you can't call it from outside the class. That might be part of the problem...

When compiling that code, I get:
error C2248: 'Salary::depNum' : cannot access private member declared in class 'Salary'

When I try using Salary::operator >> (istream &in, Salary &x) or
istream& Salary::operator >> (istream &in, Salary &x), I get error C2039: '>>' : is not a member of 'Salary'

And it doesn't matter whether I declare it as a private or public member within the class.

I got it. Thanks.

What was the solution? Just so someone else can learn from it too :0)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.