访问同一源文件中另一个.cpp的成员函数? [英] Access member function of another .cpp within same source file?

查看:120
本文介绍了访问同一源文件中另一个.cpp的成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual C ++中工作。我有两个.cpp文件在同一个源文件。如何在这个主要的.cpp文件中访问另一个类(.cpp)?

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?

推荐答案

h文件,并在.cpp文件中实现它。然后,将.h文件添加到您要使用该类的任何位置。

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.

例如

文件use_me。 h

file use_me.h

#include <iostream>
class Use_me{

   public: void echo(char c);

};

档案use_me.cpp

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;

    }

这篇关于访问同一源文件中另一个.cpp的成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆