本文介绍了尝试使用uint *&作为常量单位*&失败:类型为'uint8_t *'的表达式的类型为'const uint8_t *&'的引用无效初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码无法为我编译(gcc 4.6.3,Ubuntu 12.04):

  #include< inttypes .H> 
#include< stdio.h>

static inline void adjustBuffer(const uint8_t *& buf,size_t& bufSize,size_t len)
{
buf + = len;
bufSize - = len;

$ b uint16_t packInt(uint8_t *& buf,size_t& bufSize,int value)
{
size_t valueSize = sizeof(int);
* reinterpret_cast< int *>(buf)= value;
adjustBuffer(buf,bufSize,valueSize);
返回valueSize;


bool unpackInt(const uint8_t *& buf,size_t& bufSize,int& value)
{
value = * reinterpret_cast< const int * >(BUF);
adjustBuffer(sizeof(int));
返回true;
}

int main()
{
static const size_t BufSize = 100;
size_t bufSize = BufSize;
uint8_t buf [BufSize];
uint8_t * buf_ptr = buf;
packInt(buf_ptr,bufSize,1);
bufSize = BufSize;
int x;
unpackInt(buf,bufSize,x);
返回0;
}

我收到以下错误:

  $ make CXXFLAGS = -  Wall -gref_to_ptr 
g ++ -Wall -g ref_to_ptr.cpp -o ref_to_ptr
ref_to_ptr.cpp:In function 'uint16_t packInt(uint8_t *&,size_t&; int)':
ref_to_ptr.cpp:15:41:错误:类型为'const uint8_t *& 'uint8_t * {aka unsigned char *}'
ref_to_ptr.cpp:5:20:错误:在传递参数1'void adjustBuffer(const uint8_t *)'时,{aka const unsigned char *& &;; size_t&;; size_t)'
ref_to_ptr.cpp:在函数'bool unpackInt(const uint8_t *& ;, size_t&,int&)'中:
ref_to_ptr.cpp:22:29:error :类型为'const uint8_t *&'的非const引用无效初始化{aka const unsigned char *&}''从'unsigned int'类型的右值
ref_to_ptr.cpp:5:20:error:在传递'void adjustBuffer(const uint8_t *&; size_t& ,size_t)'
ref_to_ptr.cpp:在函数'int main()'中:
ref_to_ptr.cpp:35:30:错误:类型为'const uint8_t *& {aka const unsigned char *&}''从类型'uint8_t * {aka unsigned char *}'$'$ b $ ref_to_ptr.cpp:19:6:错误:在传递参数1'bool unpackInt(const uint8_t *&;; size_t&; int&)'
make:*** [ref_to_ptr] Error 1

似乎编译器在将uint8_t *(uint8_t *&)引用赋值给const uint8_t *& (其中IIRC是对const指针的引用)。首先,我不明白为什么它会尝试分配一个指向uint8_t的指针而不是对指针的引用。其次,不应该转换工作?您可以将uint8_t *转换为const uint8_t *,为什么不将转换引用转换为两种类型?

当然,添加一个adjustBuffer(),它需要一个const uint8_t *&工作,但我想了解为什么

解决方案

传递 uint8_t * 作为 const uint8_t *& 参数将允许函数用<$ c替换 unint8_t * $ c> const uint8_t * 。现在,在调用者期望可修改 uint8_t * 的地方有一个 const uint8_t * 。这不是保存,因为调用者可能会在函数返回后修改指向的数据。



问题与。


The following code fails to compile for me (gcc 4.6.3, Ubuntu 12.04):

#include <inttypes.h>
#include <stdio.h>

static inline void adjustBuffer(const uint8_t *&buf, size_t &bufSize, size_t len)
{
    buf += len;
    bufSize -= len;
}

uint16_t packInt(uint8_t *&buf, size_t &bufSize, int value)
{
    size_t valueSize = sizeof(int);
    *reinterpret_cast<int *>(buf) = value;
    adjustBuffer(buf, bufSize, valueSize);
    return valueSize;
}

bool unpackInt(const uint8_t *&buf, size_t &bufSize, int &value)
{
    value = *reinterpret_cast<const int*>(buf);
    adjustBuffer(sizeof(int));
    return true;
}

int main()
{
    static const size_t BufSize = 100;
    size_t bufSize = BufSize;
    uint8_t buf[BufSize];
    uint8_t *buf_ptr = buf;
    packInt(buf_ptr, bufSize, 1);
    bufSize = BufSize;
    int x;
    unpackInt(buf, bufSize, x);
    return 0;
}

I get the following errors:

$ make CXXFLAGS="-Wall -g" ref_to_ptr
g++ -Wall -g    ref_to_ptr.cpp   -o ref_to_ptr
ref_to_ptr.cpp: In function ‘uint16_t packInt(uint8_t*&, size_t&, int)’:
ref_to_ptr.cpp:15:41: error: invalid initialization of reference of type ‘const uint8_t*& {aka const unsigned char*&}’ from expression of type ‘uint8_t* {aka unsigned char*}’
ref_to_ptr.cpp:5:20: error: in passing argument 1 of ‘void adjustBuffer(const uint8_t*&, size_t&, size_t)’
ref_to_ptr.cpp: In function ‘bool unpackInt(const uint8_t*&, size_t&, int&)’:
ref_to_ptr.cpp:22:29: error: invalid initialization of non-const reference of type ‘const uint8_t*& {aka const unsigned char*&}’ from an rvalue of type ‘unsigned int’
ref_to_ptr.cpp:5:20: error: in passing argument 1 of ‘void adjustBuffer(const uint8_t*&, size_t&, size_t)’
ref_to_ptr.cpp: In function ‘int main()’:
ref_to_ptr.cpp:35:30: error: invalid initialization of non-const reference of type ‘const uint8_t*& {aka const unsigned char*&}’ from an rvalue of type ‘uint8_t* {aka unsigned char*}’
ref_to_ptr.cpp:19:6: error: in passing argument 1 of ‘bool unpackInt(const uint8_t*&, size_t&, int&)’
make: *** [ref_to_ptr] Error 1

It seems the compiler has trouble assigning a reference to uint8_t* (uint8_t *&) to a const uint8_t *& (which IIRC is a reference to pointer to const). First, I don't understand why it tries to assign a pointer to uint8_t and not a reference to a pointer. Second, shouldn't the conversion work? You can convert uint8_t * to const uint8_t *, why wouldn't converting references to both types work?

Of course, adding an adjustBuffer() that takes a const uint8_t *& works, but I'd like to understand why

解决方案

Passing an uint8_t * as a const uint8_t *& parameter would allow the function to replace the unint8_t * with a const uint8_t *. Now there is a const uint8_t *in the place where the caller expects a modifiable uint8_t *. This is not save, since the caller might modify the pointed-to data after the function returned.

The problem is the same as in this C++ FAQ lite section.

这篇关于尝试使用uint *&amp;作为常量单位*&amp;失败:类型为'uint8_t *'的表达式的类型为'const uint8_t *&amp;'的引用无效初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:36