[英]How to call main function from another header files cpp file
我想从另一个头文件 cpp 文件中调用main
函数。 其中main
包含一个头文件。 让我们调用 main.cpp 有一个头文件。 我可以从头文件 cpp 调用 main.cpp 的main
吗?
这是 main.cpp
#include "another.h"
int main()
{
cout<<"Main";
}
这是另一个.h
class another
{
public:
void another_func(void);
};
这是 another_func.cpp 单独文件
void another::another_func(void)
{
//how do i call main()
}
C++ 标准不允许在您自己的代码中调用main
。 如果你这样做了,你就处于未定义行为领域,你的整个程序毫无意义。
只有实现可以调用main
作为程序的入口点。
main
的特殊之处在于它不能被调用(包括从内部调用),它的地址不能被获取等。
所以你最好使用类似的东西
#include "another.h"
int main()
{
return Main();
}
int Main() {
std::cout<<"Main\n";
return 0;
}
这是另一个.h
class another
{
public:
void another_func(void);
};
这是 another_func.cpp 单独文件
void another::another_func(void)
{
Main();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.