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?
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;
}
Without creating header files. Use extern
modifier.
a.cpp
extern int sum (int a, int b);
int main()
{
int z = sum (2, 3);
return 0;
}
b.cpp
int sum(int a, int b)
{
return a + b;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With