调用main()之外的函数
Call a function outside main ()
我正在尝试这样做:
#include <iostream>
using namespace std;
class smth {
public:
void function1 () { cout<<"before main";}
void function2 () { cout<<"after main";}
};
call function1();
int main ()
{
cout<<" in main";
return 0;
}
call funtion2();
我想留言:"在main之前"在主体中"在主之后"
我该怎么做?
你不能。至少不是那样。您应该能够通过将代码放入类构造函数和析构函数中,然后声明一个全局变量来解决这个问题:
struct myStruct
{
myStruct() { std::cout << "Before main?n"; }
~myStruct() { std::cout << "After main?n"; }
};
namespace
{
// Put in anonymous namespace, because this variable should not be accessed
// from other translation units
myStruct myStructVariable;
}
int main()
{
std::cout << "In mainn";
}
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 代码在main()中运行,但在函数中出现错误
- 函数是否可以访问传递给main()的参数
- C++变量名(可以将 main 声明为变量,但对于其他函数名称则不然)
- 当我尝试在 Main 中调用插入函数时,它不是取数字?
- 为什么类和 main() 函数中也有动态内存分配
- "main"函数堆栈中的对象在第一个任务运行时被覆盖 (FreeRTOS)
- 为什么 'main' 函数中的局部 int 变量会自动初始化?
- 为什么在我的 main 函数中声明整数后我的整数数组中会出现错误?
- 如何在 C 中使用空的 main() 方法运行函数?
- 我在主函数的左括号上不断收到错误,消息为obj\Debug\main.o||在函数"ZN11linked_listC1Ev"中:|
- 为什么 c++ 中的 main() 函数不采用除 int 和 void 之外的任何其他返回类型
- 未解析的外部符号__imp_glewInit函数 main 中引用
- 函数main中的参数argv(int argc,char *argv [])
- 可以(通过编译器)使用多少个线程来初始化全局对象(函数main之前)
- 为什么我的编码出现错误无法在函数 main 中将 int 转换为 int *
- C/C++中函数main()的地址
- 无法在函数 main() 中创建抽象类的指针
- 无法在函数main中编译,需要Lvalue
- 如何在 c++ 中获取主函数"main()"的返回值?