我有以下代码段:
char* filename; unsigned long long int bytesToTransfer; int fd,pagesize; char *data; fd = open(filename,O_RDONLY); if (fd==NULL){ fputs ("File error",stderr); exit (1); } cout << "File Open: " << filename << endl; pagesize = getpagesize(); data = mmap((caddr_t)0,bytesToTransfer,PROT_READ,MAP_SHARED,fd,0); if (*data == -1) { fputs ("Memory error",stderr); exit (2); } cout << "Data to Send: " << data << endl;
但是当我编译时,我会收到:
error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive]
data = mmap((caddr_t)0,0);
有人能给我一个暗示什么是错的吗?
解决方法
C不执行void *的隐式强制转换,你必须明确这一点
data = static_cast<char*>(mmap((caddr_t)0,0));