Solved

Shariing Public Class functions

Posted on 2003-11-02
2
239 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now