I have a data array which is populated from the network device in big endian format. The processor on the host is an intel processor, therefore by default little endian.
I am trying to overwrite the contents of the data array received from the network device (little endian) to that of the host (big endian), however receive an lvalue error saying the structure is read only.
For simplicity sake, assume that there is just one iteration.
My code is as follows:
class FFT_Data{
private:
uint16_t data[16384];
public:
const uint16_t* Data (void) const {
return (const uint16_t*)data;
}
const void* DataBE2LE(void) const { // conversion function
// (const uint16_t*)ntohs(*data); // originally i thought this was right
data = (const uint16_t*)ntohs(*data);
}
}
and then this simplified main:
int main()
{
FFT_Data fft;
// data is received from network device
fft.DataBE2LE(); // want to convert data array from BE to LE just once
SomeFunctionThatDoesStuffWithData(fft.Data()); // function that then processes BE data.
return 0;
}
So in the above, I originally had the idea that a single line function that just did (const uint16_t*)ntohs(*data)
would be enough to modify the data array as I am operating on a pointer, however I don't think that's right as ntohs
returns a uint16_t.
Then trying to overwrite the contents of data with data = (const uint16_t*)ntohs(*data);
fails as data is a private variable and thus read only:
Compile error: FFT_Data::data' in read-only structure
CodePudding user response:
DataBE2LE()
is marked as const
on the end of its declaration, so its this
pointer is a pointer to a const FFT_Data
object, and thus its data members are implicitly const
and can't be modified.
Since DataBE2LE()
wants to modify the content of data
, simply remove the const
qualifier from the end of DataBE2LE()
.
Also, the return type of DataBE2LE()
should be void
, not (const) void*
since you are not return
ing anything.
class FFT_Data{
private:
uint16_t data[16384];
public:
const uint16_t* Data() const {
return data;
}
void DataBE2LE() {
*data = ntohs(*data);
}
};