Public Group

# How do I call a function from a class method function?

This topic is 2943 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I haven't had a reason to do it before and cannot find any mention in the tutorials or after googling.
Basically as the title suggests. I want to call a function from a method and perhaps return to it. When I try it I get an error stating that 'identifier not found'.
Do I need to redefine or prototype the function in the class??

##### Share on other sites
Some code? What you are asking is entierly doable. pastebin or source tag the code here so we can look.

For instance, you can do this:

#include <iostream>void foo();class bar{  public:    bar()    {      std::cout << "starting bar.\nCalling foo." << std::endl;      foo();      std::cout << "finished constructing bar." << std::endl;    }};void foo(){  std::cout << "inside foo!" << std::endl;}int main(){  bar b;}

For the most part, you just have to notice, that in the code above, I prototype foo before I call foo. If you are getting this error because you have things in different header/source files, then you need to continue to break things up so that you have the functions you want prototype in one header, and your class in another. You'd then include both headers in the .cpp file that implements the class.

##### Share on other sites
Thanks KulSeran.
The class is in a header file. I copy pasted the function prototype from main.cpp into the top of the header file and it now works. Never knew I had to do that?

##### Share on other sites
Unlike modern languages, C++ is generally compiled as individual modules instead of as a whole program due to historical reasons. Each module of your program (effectively, a C++ file in this context) must know the type of symbols it refers in advance; therefore, you have to provide a forward declaration of each function you use in each module.

This does not mean that the actual implementation of the symbol must reside in the same module as the declaration. The linker resolves the matching implementations when it combines the compiled modules into a whole program.

It is common that forward declarations (especially to externally-linked modules, libs) are done in header files, which are then #included (effectively merged before compilation) in the code files that need the declarations of said symbols. This helps to separate interfaces from implementations.

Modern programming platforms simply put a lot more effort into finding the symbols automatically. This could not be afforded due to several technical reasons back when the C and C++ compiler/linker scheme was designed; yet, the system is too widely used to be overhauled completely to match all modern expectations. It is worth remembering that available computing power has increased exponentially from those days.

##### Share on other sites
this is the easy part of class's lolz..

1. 1
2. 2
3. 3
Rutin
20
4. 4
frob
20
5. 5

• 32
• 13
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
632559
• Total Posts
3007080

×