| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 854 人关注过本帖
标题:请教C++的类型转换的问题
只看楼主 加入收藏
q201q5
Rank: 1
等 级:新手上路
帖 子:14
专家分:3
注 册:2016-2-23
结帖率:50%
收藏
 问题点数:0 回复次数:0 
请教C++的类型转换的问题
c++我不太熟,用c语言编译没问题,但c++不行。
大概是这样的,我需要把一个整数转成指针,然后再转成整数,带式类似于
程序代码:
#define U2P(u)  ((void*)(unsigned long)(u))
#define P2U(p)  ((unsigned long)(void*)(p))

#define A   U2P(2)

int foo(void *p)
{
    switch(P2U(p)) {
    case P2U(A): return 1;
    default: return 0;
    }
}

但是编译出现
$ g++ -c a.cc
a.cc: In function 'int foo(void*)':
a.cc:1:42: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
 #define U2P(u)  ((void*)(unsigned long)(u))
                                          ^
a.cc:2:41: note: in definition of macro 'P2U'
 #define P2U(p)  ((unsigned long)(void*)(p))
                                         ^
a.cc:4:13: note: in expansion of macro 'U2P'
 #define A   U2P(2)
             ^
a.cc:9:11: note: in expansion of macro 'A'
  case P2U(A): return 1;
           ^
a.cc:2:42: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
 #define P2U(p)  ((unsigned long)(void*)(p))
                                          ^
a.cc:9:7: note: in expansion of macro 'P2U'
  case P2U(A): return 1;
       ^

我应该怎么改才是正确的?
搜索更多相关主题的帖子: c语言 color 
2016-04-03 11:29
快速回复:请教C++的类型转换的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014017 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved