[英]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.