Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this