Alex Rivera | Logout

Access member function of another .cpp within same source file?

Asked 2009-08-20T04:58:50.283
9

I am working in Visual C++. I have two .cpp files in the same source file. How can I access another class (.cpp) function in this main .cpp?

Edit
Report

1 Answer

11

You should define your class in a .h file, and implement it in a .cpp file. Then, include your .h file wherever you want to use your class.

For example

file use_me.h

#include <iostream>
class Use_me{

   public: void echo(char c);

};

file use_me.cpp

#include "use_me.h" //use_me.h must be placed in the same directory as use_me.cpp

void Use_me::echo(char c){std::cout<<c<<std::endl;}

main.cpp

#include "use_me.h"//use_me.h must be in the same directory as main.cpp
    int main(){
       char c = 1;

       Use_me use;
       use.echo(c);

       return 0;

    }
answered 2009-08-20T05:17:58.950

Your Answer