最近做的项目,需要把代码从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,好奇葩。

最新文章

  1. C#文件目录操作完全手册
  2. css3设置边框属性
  3. 如何调试R程序(转载)
  4. 方法:Linux 下用JAVA获取CPU、内存、磁盘的系统资源信息
  5. zTree实现地市县三级级联报错(一)
  6. 1004 Anagrams by Stack
  7. CSS3中文手册基础知识
  8. JavaSE基础问答
  9. python 验证码识别库pytesseract的使用
  10. linux上修改mysql登陆密码
  11. python -----一个简单的小程序(监控电脑内存,cpu,硬盘)
  12. web font
  13. 学号:20165239 预备作业3 Linux安装及学习
  14. 《ERP系统修正数据的sql文件》
  15. 电脑不能上网win7 解决办法
  16. iTerm通过堡垒机自动登录服务器
  17. 中国将有可能在全球化的背景下收获新的人口红利:3星|《&lt;财经&gt;2019:预测与战略》
  18. CPP_const&amp;static
  19. 在系统启动时,Windows Vista 中、 在 Windows 7 中,Windows Server 2008 中和在 Windows Server 2008 R2 中的 497 天后未关闭 TIME_WAIT 状态的所有 TCP/IP 端口
  20. JS获取周、月、季度日期

热门文章

  1. 查看pid
  2. Java基础知识强化之集合框架笔记20:数据结构之 栈 和 队列
  3. css考核点整理(十二)-能描述下你在项目中都用到了哪些符合逐渐增强和优雅降级的理念的技巧吗
  4. 在Mac OS X下安装Android Studio
  5. Rapha&#235;l.js学习笔记
  6. 10.11 noip模拟试题
  7. codevs1024一塔湖图(丧心病狂的建图)
  8. ecshop了解01
  9. Jquery 替换全部花括号
  10. 分享最近写的一个存储过程 SOLVE_LOCK