Data member is inaccessible?

I am getting started on a polymorphic program for my advanced C++ class, but Visual Studio is complaining about a data member in one of my classes being inaccessible. I can't see where the problem is occurring. I was wondering if another set of eyes may be able to see where I am going wrong...

Here are the header and definition files for the class where the problem is occurring:

Name.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef NAME_H
#define NAME_H

#include <string>
using std::string;

class Name
{
	friend ostream &operator<<(ostream &out, const Name &name);
	friend istream &operator>>(istream &in, const Name &name);
public:
	Name(const string &initName = "");
private:
	string contactName;
};

#endif 


Name.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Name.h"
#include <iostream>
#include <iomanip>
using std::ostream;
using std::istream;
using std::setw;

Name::Name(const string &initName)
{
	contactName = initName;
}

ostream &operator<<(ostream &out, const Name &name)
{
	out << name.contactName;
	return out;
}

istream &operator>>(istream &in, const Name &name)
{
	in >> setw(25) >> name.contactName;
        return in;
}


The problem is appearing in the overloaded operator<< and operator >> functions, when I try to read or write to the name.contactName data member. Visual studio is telling me that data member is inaccessible. Any ideas?
Well, the declaration and definition look ok to me. I guess it just might be your friend declarations.

As you are including <fstream> after your header, name.h, your class might not know who its friends really are.
Visual Studio is (indirectly) reporting it can't find the non-const version of operator>>.
Remove the const declaration on the Name parameter in operator>>

friend istream& operator>>(istream& in, Name& name);

Cheers,
JimbO
Last edited on
Topic archived. No new replies allowed.