主命名空间 [英] main in namespace

查看:70
本文介绍了主命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不编译? 链接器找不到main,但是为什么会这样?

Why doesn't this compile? The linker can't find main, but why is this the case?

namespace somenamespace{

int main(void){
 return 0;
}

}

推荐答案

3.6.1/1-"程序应包含一个名为main 的全局函数,该函数 是指定的开始 程序.它是实现定义的 是否有独立的程序 需要环境来定义 主功能. [注意:在 独立的环境,启动和 终止是实现定义的; 启动包含执行 命名空间对象的构造函数 具有静态存储期限的范围; 终止包含执行 静态对象的析构函数 储存期限. —尾注]

3.6.1/1 - "A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function. [ Note: in a freestanding environment, start-up and termination is implementation-defined; startup contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. —end note ]

您的示例具有"main"(用作程序入口点)作为名称空间功能,因此您的代码格式错误.这并不意味着不能像您那样定义函数"main".这仅表示自立程序托管程序

Your example has 'main' (intended as the program entry point) as a namespace function and hence your code is ill-formed. This does not mean that a function 'main' can not be defined as you did. It just means that a global namespace scope definition of 'main' in accordance with the Standard defined signature is required for a free standing program. hosted program

这篇关于主命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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