Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't access protected data member of Tape type

Status
Not open for further replies.

clarkboy

Technical User
Dec 11, 2002
1
0
0
US
Created a structure of Tape that holds (stars, producer, etc...). I created a protected data member in my Video class of Tape type named "info". When implementing a member function of the Video class I'm trying to access "info" through a reference of a Video object. I keep getting this error telling me I can't access protected member of Video class. This is the segment of the code:

void setInfo(Video& obj)
{

string star, produce, direct, productCom, typeM;

cout << &quot;Enter stars in movie: &quot;;
getline(cin, star);
obj.info.stars = star;

I keep this error.
error C2248: 'info' : cannot access protected member declared in class 'Video' c:\eric\homework\video.h(47) : see declaration of 'info'

If someone could explain why I'm getting this error it would be greatly appreciated!!!!
 
maybe you meant:

[tt]void Video::setInfo(Video& obj)
{

string star, produce, direct, productCom, typeM;

cout << &quot;Enter stars in movie: &quot;;
getline(cin, star);
obj.info.stars = star;
}[/tt]
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
You cannot access a protected member of a class except from a derived class. Since you are trying to access it from outside the Video class which is not derived from the Video class, what you need here is an setter function that would set the info member of the video class.

Something like:

Video::SetStar(const string& star)
{
info.stars = star;
}

and call this function from your setInfo function.

void setInfo(Video& obj)
{
string star, produce, direct, productCom, typeM;

cout << &quot;Enter stars in movie: &quot;;
getline(cin, star);
obj.Set(star);
...
}

 
Insert public: before definition of info in Video.h
 
Proper OO programming requires you to use accessor functions for most, if not all, member variables that are called from outside the inheritance tree.

What this means is that if you have class A with a member variable, you should declare that member variable private unless you plan to subclass class A. In this case, you declare the member var as protected to promote polymorphism.

Any member variables that are accessed outside this inheritance, as you are trying to achieve, should be accessed using accessor functions. For example:

Class A[tt]
class A
{
private:
int nVar1;
protected:
int nVar2;
public:
int GetVar1() { return nVar1 };
int GetVar2() { return nVar2 };
}
[/tt]

Class B[tt]
class B: public A // inherited from class A
{
private:
A ClassA;
public:
void DoSomething( void );
}

void B::DoSomething( void );
{
cout << nVar2; // this is okay as nVar2 is protected
// and we are in a derived class
cout << nVar1; // not allowed, nVar1 is private and can
// only be accessed by member functions
// within A

cout << ClassA.GetVar1() // Okay since GetVar1 is a
// public function
}
[/tt]
Class C[tt]
Class C // Not inherited
{
public:
void DoSomething( void );
private:
A ClassA;
}

C::DoSomething( void )
{
cout << ClassA.nVar1; // Invalid - nVar1 is private
// in class A
cout << ClassA.nVar2; // Invalid - nVar2 is protected
// in class A and we are not in
// an inherited class
cout << ClassA.GetVar1() // okay since GetVar1 is a public
// function
cout << ClassA.GetVar2() // okay since GetVar2 is a public
// function
}
[/tt][/tt]
Probably as clear as mud, but it might help!

Jim
 
&quot;When implementing a member function of the Video class I'm trying to access &quot;info&quot; through a reference of a Video object.&quot;

Perhaps I mistunderstood when I first answered this query but a member function in my book does have access to private and protected members of the same class. However, if the scope resolution operator is missing like it was originally, the function becomes a global function and therefore is not a member of the said class - and as such as no access to protected/private members.

[tt]void Video::setInfo(Video& obj)[/tt]
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top