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);


and the definition:

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

	return in;
4 Years
Discussion Span
Last Post by sfurlow72

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.