Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Shariing Public Class functions

Posted on 2003-11-02
2
Medium Priority
?
249 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

927 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