member of class inaccessible

i am writing a program where i have to print float array. here is my header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef FLOAT ARRAY_H
#define FLOAT ARRAY_H
class floatArray
{
	friend std::istream& operator>>(std::istream& is,floatArray& v);
	friend std::ostream& operator<<(std::ostream& os,const floatArray& v);
public:
	floatArray();
	floatArray(const floatArray& rhs);
private:
	float*mData;
	int mSize;
};
#endif 

the implementation file is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include"floatArray.h"
#include<iostream>
using namespace std;
floatArray::floatArray()
{
	*mData=0.0f;
}
floatArray::floatArray(const floatArray& rhs)
{
	int len=rhs.mSize;
	mData=new float[len];
	for(int i=0;i<len;i++)
		rhs.mData[i]=mData[i];
}
std::ostream& operator<<(std::ostream& os,const floatArray& v)
{
	for(int i=0;i<v.mSize;i++)
		cout<<v.mData[i];
	return os;
}
std::istream& operator>>(std::istream& is,floatArray& v)
{
	for(int i=0;i<v.mSize;i++)
	{
		cout<<"["<<i<<"]="<<"\n";
		cin>>v.mData[i];
	}
	return is;
}

problem it is giving error that floatArray::mSize not accessible and
floatArray::mData is not accessible.
any help would be appreciated.
Because the iostream operators aren't friends of your class. They don't have rights to access your private members.
could you please elaborate!! i think i have included them as friend
Sorry, I was wrong on that. Friend's only have access to protected. Private is for the class, and class only, to have access to. Protected is available for friends and derived I believe.

Edit: I was right originally -.-
Last edited on
std::istream and std::ostream has not been declared when you mention them on line 5-6 in floatArray.h.

Add the includes in floatArray.h.
1
2
#include <istream>
#include <ostream> 
@Volatile Pulse
Friends can access private members.
that solved my problem. thanks!!
If the code you have posted is truly represntative of your code, then the compiler should have displayed more than floatArray::mSize not accessible and
floatArray::mData is not accessible.


You need to do the following.

1
2
3
4
5
#ifndef FLOAT ARRAY_H
#define FLOAT ARRAY_H
#include <iostream> //addd this here 
class floatArray
{



While we have your attention:
In the constructor
1
2
3
4
floatArray::floatArray()
{
	*mData=0.0f; //Error
}


I'm sure you plan to do this - but as you are allocating/deallocating
memory you will need an appropriate assignment operator= function and an appropriate
destructor.
Last edited on
yes compiler did posted more errors!! but as you and Peter87 said including #include<iostream> solved my problem. thanks guys.
Topic archived. No new replies allowed.