从VC到g++遇到的事
2024-08-26 12:39:34
最近做的项目,需要把代码从VC移植到g++下编译,在这个过程中,遇到了几个平台相关的问题——在VC下顺利编译的代码,但在g++中编译报错。
这里贴出来给大家分享一下:
1. 枚举类型
问题代码
enum MyWeek
{
Monday,
Tuesday,
}; MyWeek mw = MyWeek::Monday;
VC
d:\VC\main.cpp(17) : warning C4482: 使用了非标准扩展: 限定名中使用了枚举“MyWeek”
g++
main.cpp:11:14: error: ‘MyWeek’ is not a class or namespace
MyWeek mw = MyWeek::Monday;
^
总结
MyWeek::Monday不是C++标准,所以应该尽量避免这种写法。有一个建议是在命令枚举时加上枚举类型的前缀,这样可以有效避免枚举名重复的情况。比如
enum MyWeek
{
MW_Monday,
MW_Tuesday,
};
2. std::map::cbegin()
问题代码
std::map<int, int>::const_iterator cIter = testMap.cbegin();
VC
顺利编译通过
g++
main.cpp:7:53: error: ‘class std::map<int, int>’ has no member named ‘cbegin’
std::map<int, int>::const_iterator cIter = testMap.cbegin();
^
总结
这个结果是在g++4.8.2跑的,g++下面,map::cbegin, map::cend之类的方法都是没有的。
3. #include<map >
include的头文件中如果存在空格,在VC下是没有问题的,但是在g++下会报错。
main.cpp:2:16: fatal error: map : 没有那个文件或目录
#include <map >
^
compilation terminated.
4. 类成员函数声明前加上了类修饰符
问题代码
class A
{
void A::M();
};
VC
顺利编译通过
g++
main.cpp:6:7: error: extra qualification ‘A::’ on member ‘M’ [-fpermissive]
void A::M();
^
5. >>
问题代码
std::map<int, std::map<int, int>> testMap;
VC
顺利编译通过
g++
main.cpp:4:32: error: ‘>>’ should be ‘> >’ within a nested template argument list
std::map<int, std::map<int, int>> testMap;
^
总结
这个问题经常遇到啊,在写模板代码的时候要注意,两个’>’不能挨到一起,中间要加个空格。
6. itoa
g++只有atoi,没有itoa,好奇葩。
最新文章
- C#文件目录操作完全手册
- css3设置边框属性
- 如何调试R程序(转载)
- 方法:Linux 下用JAVA获取CPU、内存、磁盘的系统资源信息
- zTree实现地市县三级级联报错(一)
- 1004 Anagrams by Stack
- CSS3中文手册基础知识
- JavaSE基础问答
- python 验证码识别库pytesseract的使用
- linux上修改mysql登陆密码
- python -----一个简单的小程序(监控电脑内存,cpu,硬盘)
- web font
- 学号:20165239 预备作业3 Linux安装及学习
- 《ERP系统修正数据的sql文件》
- 电脑不能上网win7 解决办法
- iTerm通过堡垒机自动登录服务器
- 中国将有可能在全球化的背景下收获新的人口红利:3星|《<;财经>;2019:预测与战略》
- CPP_const&;static
- 在系统启动时,Windows Vista 中、 在 Windows 7 中,Windows Server 2008 中和在 Windows Server 2008 R2 中的 497 天后未关闭 TIME_WAIT 状态的所有 TCP/IP 端口
- JS获取周、月、季度日期