c++ - 从类中的 main 调用函数

标签 c++ function class program-entry-point

<分区>

所以我在main中定义了一个函数,我想在一个类中重用同一个函数...所以我不得不在类中重新创建它。我的问题是,有没有一种方法可以从 main 调用该函数,而不必将其复制并粘贴到类中......?

主要.cpp

#include "array_class.h"
#include "stack_class.h"
#include "queue_class.h"

using namespace std;


void displayText (std::string text, int sleepTime);

int main(){
} 

现在我打算在 array_class 中调用 displayText

最佳答案

在标题中声明函数,然后在需要的地方链接到它

foo.h

int foo();

foo.cpp

#include "foo.h"
int foo()
{
    return 5;
}

类.cpp

#include "foo.h"
int x = foo();  // just called the function

main.cpp

#include "foo.h"
int main()
{
    int y = foo(); // called the function again
    std::cout << y << std::endl;
}

关于c++ - 从类中的 main 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24843987/

相关文章:

function - 函数式编程中的所有纯函数都是连续的吗?

function - Haskell 中 lambda 函数的解释

c++ - 如何调用将不同类模板作为参数列表的函数模板

c++ - 从迭代器构造的反向迭代器是其先前的迭代器吗?

c++ - 不允许嵌套函数,但为什么允许嵌套函数原型(prototype)? [C++]

c++ - 仅使用纹理坐标的棋盘着色器

windows - 如何确定调用函数是否需要管理权限

c++ - 类型类的 vector (地址簿程序)

python - app.py 脚本中的几行我不明白

ios - Swift Objective-C 运行时类命名