#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(常量無效*)':8 行:錯誤: 'const的無效*' 不是指針到對象類型 由於重大錯誤而終止編譯。
嘗試過鑄造,如下所示。它並沒有幫助。請提供建議。
#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(常量無效*)': 第9行:錯誤:從的static_cast類型 'const的空隙*' 爲類型 '字符*' 擅自拋棄常量性 彙編由於-Wfatal-終止錯誤
您不能取消引用* cv *'void'的指針。 – rightfold
'void *'不能被解除引用。它必須被轉換成具體類型的指針。 – sgarizvi