c++ - 访问同一源文件中另一个 .cpp 的成员函数?

标签 c++ class

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

最佳答案

您应该在 .h 文件中定义您的类,并在 .cpp 文件中实现它。然后,在您想使用类的任何地方包含您的 .h 文件。

例如

文件 use_me.h

#include <iostream>
class Use_me{

   public: void echo(char c);

};

文件 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;}

主要.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;

    }

关于c++ - 访问同一源文件中另一个 .cpp 的成员函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1304035/

相关文章:

Python 覆盖字符串 __hash__

PHP:从数组中调用一个类?

c++ - 灵气中可选解析器的使用

c++ - 获取 Visual Studio 项目名称(或 exe 文件名称)以在 C++ 程序中使用它

c++ - 具有一些非公共(public)部分的模板函数

c++ - 我可以在同一个类中创建对象吗?

css - 无论如何将CSS类分组在一起?

C++在没有sqrt函数循环故障的情况下找到平方根

c++ - c vector 可以存储多种数据类型吗?

Python 类 : Why can't I use the method len() inside __eq__(self, 其他)?