一个C++程序中的关于指针的疑问

thegodofwar 2009-10-23 04:23:41
#include<iostream>
#include<string>
using namespace std;
double a,b;
int main() {
a=78.0;
cin>>a;
void *p;
p=&a;
b=12.0;
cin>>b;
void *p1;
p1=&p1;
cout<<(*p1+*p)<<endl;//该行为什么不能执行,出错了?????????????
return 0;
}
...全文
184 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
qychjj 2009-10-24
  • 打赏
  • 举报
回复
还是学习了点
lizzoe 2009-10-24
  • 打赏
  • 举报
回复
正解

[Quote=引用 4 楼 lianshaohua 的回复:]
void *p1;
p1=&p1;

cout < <*p1+*p < <endl;

编译器不知道p1中存的是什么类型的,所以无法解引用,需要转换为具体的类型指针再解引用;
[/Quote]
sea_sharka_17 2009-10-24
  • 打赏
  • 举报
回复
类函数的多态
int add(int, int)
float add(float, flaot)
sea_sharka_17 2009-10-24
  • 打赏
  • 举报
回复
你这要是对了 那我就很稀奇了
void是指无符号 为什么是无符号指针呢 因为 指针的定长是4个字符(int*)(char*)都是 但指针所指的内存块长是多少呢 就需要符号类型进行定义
char a = 'a';
int b = 5;
void* p = &b;
cout << (char*)p << endl;
这个时候编译器就会以1个字符 去获取指针p指向的内存块
thegodofwar 2009-10-24
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 dwhanx_p 的回复:]
cout < <(*p1+*p) < <endl;//该行为什么不能执行,出错了?????????????

改成这样:
cout < <((*(double *)p1) + (*(double *)p)) < <endl;//

[/Quote]哥们你这种改法不对呀,而且不是我的本意呀,我的本意是实现不管你从键盘上输入两个int型的数或者是double型的数或者是其它,实现它两相加,但是我又不想用一般的那种重载办法,我的思路来自void *p,这样定义,p它就可以是指向一个未知数据类型的指针,我想利用这点来实现重载,貌似有点不容易,虽然用到的知识很常见,但貌似有的细节不能实现,请高手帮帮忙再研究研究看可行不?????????????
dwhanx_p 2009-10-24
  • 打赏
  • 举报
回复
cout < <(*p1+*p) < <endl;//该行为什么不能执行,出错了?????????????

改成这样:
cout < <((*(double *)p1) + (*(double *)p)) < <endl;//
thegodofwar 2009-10-23
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 lihan6415151528 的回复:]
p1=&p1;

不知道你这里要表达什么意思
[/Quote]
不好意思,打错了,把p1=&b;但是问题还是在原来那个位置?????????
lihan6415151528 2009-10-23
  • 打赏
  • 举报
回复
p1=&p1;

不知道你这里要表达什么意思
thegodofwar 2009-10-23
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 fanghaobing 的回复:]
//试试这个,非常基础的东西
#include <iostream>
#include <string>
using namespace std;
double a,b;
int main()
{
  a=78.0;
  cin>>a;
  double *p;
  p=&a;
  b=12.0;
  cin>>b;
  double *p1;
  p1=p;
  cout < <(*p1+*p) < <endl;
  return 0;
}

//你原来的代码不知道你想做什,即然付了值,又要重新键盘输入
int main() {
  a=78.0;
  cin>>a;
  void *p;
  p=&a;
  b=12.0;
  cin>>b;
  void *p1;  //void* 自动转换为double*在编译时是通不过的
  p1=&p1;    //这可能是你粗心,把一个空指针的地址付给这个空指针,你想干嘛
  cout < <(*p1+*p) < <endl;//该行为什么不能执行,出错了?????????????
  return 0;
}


[/Quote]谢谢了,哥们,不过你那样做我也会,我的本意是实现不管你从键盘上输入两个int型的数或者是double型的数或者是其它,实现它两相加,但是我又不想用一般的那种重载办法,我的思路来自void *p,这样定义,p它就可以是指向一个未知数据类型的指针,我想利用这点来实现重载,貌似有点不容易,虽然用到的知识很常见,但貌似有的细节不能实现,请高手帮帮忙再研究研究看可行不?????????????
zcw364 2009-10-23
  • 打赏
  • 举报
回复
编译出现:
error C2100: illegal indirection
error C2100: illegal indirection
error C2110: '+' : cannot add two pointers
两个指针不能相加?
fanghaobing 2009-10-23
  • 打赏
  • 举报
回复

//试试这个,非常基础的东西
#include <iostream>
#include <string>
using namespace std;
double a,b;
int main()
{
a=78.0;
cin>>a;
double *p;
p=&a;
b=12.0;
cin>>b;
double *p1;
p1=p;
cout <<(*p1+*p) <<endl;
return 0;
}

//你原来的代码不知道你想做什,即然付了值,又要重新键盘输入
int main() {
a=78.0;
cin>>a;
void *p;
p=&a;
b=12.0;
cin>>b;
void *p1; //void* 自动转换为double*在编译时是通不过的
p1=&p1; //这可能是你粗心,把一个空指针的地址付给这个空指针,你想干嘛
cout < <(*p1+*p) < <endl;//该行为什么不能执行,出错了?????????????
return 0;
}

thegodofwar 2009-10-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 supermegaboy 的回复:]
p1和p都是指向void空对象的指针,void不是完整对象类型,是不能在一个需要进行左值转换的场合使用间接运算符的。

标准的规定:

5.3.1 Unary operators

1 The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is “pointer to T,” the type of the result is “T.”
[Note: a pointer to an incomplete type (other than cv void ) can be dereferenced. The lvalue thus obtained can be used in limited ways (to initialize a reference, for example); this lvalue must not be converted to an rvalue, see 4.1. ]
[/Quote]哥们,你的意思是不是说,一旦“void *p和void *p1”这样定义了一个指向不知道数据类型的指针变量,就不可以用*p+*p1,*p,还有关于p的&取地址符了??????麻烦你再讲明白点,还没搞清,非常感谢!!!!!
thegodofwar 2009-10-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 neu_yanggx 的回复:]
p1=&p1; ????


[/Quote]不好意思,打错了,把p1=&b;但是问题还是在原来那个位置?????????
飞天御剑流 2009-10-23
  • 打赏
  • 举报
回复
p1和p都是指向void空对象的指针,void不是完整对象类型,是不能在一个需要进行左值转换的场合使用间接运算符的。

标准的规定:

5.3.1 Unary operators

1 The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is “pointer to T,” the type of the result is “T.”
[Note: a pointer to an incomplete type (other than cv void ) can be dereferenced. The lvalue thus obtained can be used in limited ways (to initialize a reference, for example); this lvalue must not be converted to an rvalue, see 4.1. ]
Julykey 2009-10-23
  • 打赏
  • 举报
回复
LS的正确
Julykey 2009-10-23
  • 打赏
  • 举报
回复
不改的话在Visual Studio2005下的调试报错: error C2100: illegal indirection
ztenv 版主 2009-10-23
  • 打赏
  • 举报
回复
void *p1;
p1=&p1;

cout<<*p1+*p<<endl;

编译器不知道p1中存的是什么类型的,所以无法解引用,需要转换为具体的类型指针再解引用;
Julykey 2009-10-23
  • 打赏
  • 举报
回复
问题是出在void *p和void *p1上,改成int *就行了。不过我好像看到别人也这么用过怎么没错
xiaozhu_zy 2009-10-23
  • 打赏
  • 举报
回复
p1=&p1;
这句话有问题吧?
neu_yanggx 2009-10-23
  • 打赏
  • 举报
回复
p1=&p1; ????

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧