我在使用Visual C++.我在同一个源文件中有两个.cpp文件.如何在这个主.cpp中访问另一个类(.cpp)函数?
Tom*_*Tom 11
您应该在.h文件中定义您的类,并在.cpp文件中实现它.然后,将.h文件包含在您想要使用您的课程的任何位置.
例如
file use_me.h
#include <iostream>
class Use_me{
public: void echo(char c);
};
Run Code Online (Sandbox Code Playgroud)
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;}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)