【发布时间】:2012-11-22 18:22:33
【问题描述】:
#include <iostream>
using namespace std;
void fn(const void *l) {
// How to print the value of l. The below line is giving error
cout << "***" << *l;
}
int main() {
cout << "Hello World!";
int d = 5;
fn((char *) &d);
return 0;
}
错误:
在函数'void fn(const void*)'中: 第 8 行:错误:'const void*' 不是指向对象的指针类型 编译因 -Wfatal-errors 而终止。
如下所示尝试了铸造。它没有帮助。请提供建议。
#include <iostream>
using namespace std;
void fn(const void *l) {
// How to print the value of l. The below line is giving error
int *pInt = static_cast<char*>(l);
cout << *pInt;
}
int main() {
cout << "Hello World!";
int d = 5;
fn((char *) &d);
return 0;
}
在函数'void fn(const void*)'中: 第 9 行:错误:从类型 'const void*' 到类型 'char*' 的 static_cast 抛弃了 constness 编译因 -Wfatal-errors 而终止
【问题讨论】:
-
您不能取消引用指向 cv
void. 的指针。 -
void*无法取消引用。它必须被转换成一个具体类型的指针。
标签: c++