在类中从main调用函数

Calling a function from main in a class

本文关键字:调用 函数 main      更新时间:2023-10-16

所以我在main中定义了一个函数,我想在类中重用相同的函数…所以我不得不在课堂上重做一遍。我的问题是,有没有一种方法可以从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(){
} 

现在我打算调用displayText,比如,array_class

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

foo。

int foo();

foo.cpp

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

class.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;
}