Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5994021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:45:31+00:00 2026-05-22T23:45:31+00:00

i have two questions that are fairly small and related so i will put

  • 0

i have two questions that are fairly small and related so i will put them both in the same question.

i have been experimenting with classes and i was attempting to access a class in another file that wasn’t in a class so for example.

//class 1 .cpp
void Class1::function1()//another error 
{
    function()
}


//main.cpp

void function()
{
//stuff happens
}

is there a way to-do this? or would i need to add this function to a class to get it to work. also how would you go about creating a function that receives a function as it parimetre? for example function(function2())

i am simply trying to access a function from a class as it would make my code easier to use later if the function that i am using doesn’t get added to a class. with regards to the seconds question i which to create a function that receives a time and a function as an argument. it will wait for the specified time then execute the program

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:45:32+00:00Added an answer on May 22, 2026 at 11:45 pm

    How to access a function in another file?
    Depends on the type of function, there can be to cases:

    1. Accessing class member functions in another file(Translation Unit):
    Obviously, you need to include the header file, which has the class declaration in your caller translation unit.

    Example code:

    //MyClass.h
    
    class MyClass
    {
        //Note that access specifier
        public:
            void doSomething()
            {
                 //Do something meaningful here
            }
    
    };
    
    #include"MyClass.h"    //Include the header here
    //Your another cpp file
    int main()
    {
        MyClass obj;
        obj.doSomething();
        return 0;
    }
    

    2. Accessing free functions in another file(Translation Unit):

    You do not need to include the function in any class, just include the header file which declares the function and then use it in your translation unit.

    Example Code:

    //YourInclude.h
    
    inline void doSomething() //See why inline in @Ben Voight's comments
    {
        //Something that is interesting hopefully
    }
    
    //Your another file
    
    #include"YourInclude.h"
    
    int main()
    {
        doSomething()
        return 0;
    }
    

    Another case as pointed out by @Ben in comments can be:
    A declaration in the header file, followed by a definition in just one translation unit

    Example Code:

    //Yourinclude File
    void doSomething();  //declares the function
    
    //Your another file
    include"Yourinclude"
    void doSomething()   //Defines the function
    {
        //Something interesting
    }
    
    int main()
    {
        doSomething();
        return 0;
    }
    

    Alternately, a messy way to do this can be to just mark the function as extern in your another file and use the function.Not recommended but a possibility so here is how:

    Example Code:

    extern void doSomething();
    
    int main()
    {
        doSomething();
        return 0;
    }
    

    How would you go about creating a function that receives a function as it parameter?
    By using function pointers
    In a nutshell Function pointers are nothing but pointers but ones which hold address of functions.

    Example Code:

    int someFunction(int i)
    {
        //some functionality
    }
    
    
    int (*myfunc)(int) = &someFunction;
    
    
    void doSomething(myfunc *ptr)
    {
        (*ptr)(10); //Calls the pointed function
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have four tables user-question contains two columns: questionID, userID, the questions that the
A quick technical question- I have two queries that output some of the same
I have a question that is more related to how ASP.NET works and will
I have two questions: (1) I learned somewhere that -O3 is not recommended with
I have two models (questions and answers) that slightly follow the popular Railscasts :
i have two questions, that i encountered in my learning project 1) Setting: i
I have two questions. 1: At the moment I have two model classes that
I actually have two questions regarding exception/error handling in the iPhone app that I
1) My question is that, when i have two large table which cannot be
I have a question: I have two MSSQL tables, items and states, that are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.