Solved

Shariing Public Class functions

Posted on 2003-11-02
2
243 Views
Last Modified: 2010-04-01
Hello,  Im new to c++, so....  

I have 2 headerfiles, one class in each, with various public functions (Accesors etc..)  The functions corresponding with each header file are defined in thier own seperate .cpp files.  And there is a main of course.  So thats five files..  

I believe what I am trying to do is aggregation, I want to access a private value from one object to be used in a method from the other object.   So what I assumed would work is calling the public accessor method from the class that I want to retrieve the value from.  But...  I am getting the following compilation error

In function 'void feed()'
'getAttributes" undeclared (first use this function)

I have each seperate header included. so that cant be the problem.  I know the problem is when I try and call that function its not aware of its existance because I am calling it from inside another class..  My question:  Is there any way that I can allow one class to retrieve a private variable from another using the public accessor method from the class where the private value resides?

Many Thanks,
DJ.
0
Comment
Question by:DMJohns
2 Comments
 
LVL 11

Accepted Solution

by:
bcladd earned 125 total points
ID: 9666554
(1) Class A has two files:
     classA.h
     classA.cpp

Class B has two files:
    classB.h
    classB.cpp

In Class B you want to access a member function of class A? It looks from the error that you don't use a variable of type B in the function inside class A. That is getAttributes cannot be called without a pointer, reference or object of type B:

    B sometB;
    someB.getAttributes(); // this should be fine.

-bcl
0
 
LVL 6

Expert Comment

by:durgaprasad_j
ID: 9666651
Try using 'friend' functions , so that u can access one function in other class .

Thanks
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question