如何从单独的cpp文件中调用主cpp文件中的main()? [英] How do I call main() in my main cpp file from a seperate cpp file?

查看:290
本文介绍了如何从单独的cpp文件中调用主cpp文件中的main()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个向用户显示消息并询问他们是否要返回程序开头的类,但是message函数与main()所在的类位于不同的类中。如何从其他类访问main()函数?

I'm making a class that displays a message to the user and asks them if they want to return to the start of the program, but the message function is in a separate class from where my main() is located. How do I access the main() function from a different class?

这是我要执行的操作的一个示例:

main。 cpp文件:

This is an example of the stuff I want to do:
main.cpp file:

int main()
{
    Message DisplayMessage;
    DisplayMessage.dispMessage();
    return 0;
} 

Message.cpp文件:

Message.cpp file:

void dispMessage(void)
{
    cout << "This is my message" << endl;
    //now how do I call main again in the main.cpp file?
}

谢谢!

推荐答案

在C ++中,程序本身调用 main 是非法的,因此简单的答案是不要。您需要重构代码,最简单的转换是在 main 中编写一个循环,但其他替代方法可能包括在 main中分解逻辑转换为在标头中声明的另一个函数,您可以调用该函数。

In C++ it is illegal for a program to call main itself, so the simple answer is you don't. You need to refactor your code, the simplest transformation is to write a loop in main, but other alternatives could include factoring the logic out of main into a different function that is declared in a header and that you can call.

这篇关于如何从单独的cpp文件中调用主cpp文件中的main()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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