cppkafka icon indicating copy to clipboard operation
cppkafka copied to clipboard

How to produce array of float64

Open ahoora08 opened this issue 7 years ago • 1 comments

I have an float64 array of size 129. I tried to produce it to Kafka:

       totalarray[0] = t0;
	for (i = 0; i < 40; i++) {
		totalarray[i + 1] = readanalog[i];
	}
	for (int j = 0; j < 88; j++) {
		totalarray[j+41] = data1[j];
	}
        builder.topic(TOPIC_NAME);
        builder.payload({totalarray, 129});
        producer.produce(builder);

But I got the errro:

In file included from /usr/local/include/cppkafka/producer.h:36:0,
                 from main.cpp:12:
/usr/local/include/cppkafka/buffer.h: In instantiation of ‘cppkafka::Buffer::Buffer(const T*, size_t) [with T = double; size_t = long unsigned int]’:
main.cpp:145:42:   required from here
/usr/local/include/cppkafka/buffer.h:78:9: error: static assertion failed: sizeof(T) != sizeof(DataType)
         static_assert(sizeof(T) == sizeof(DataType), "sizeof(T) != sizeof(DataType)");
         ^

How can I fix the error?

ahoora08 avatar Aug 07 '18 12:08 ahoora08

Constructing Buffers from arbitrary pointers is not allowed unless the type the pointer points to is one byte long. So you can basically write chars, uint8_t, etc, but writing float is a bad idea given the representation of them can be different on one platform and another.

If you still want to do this, you can cast the pointer to something like a char*. e.g.

const char* ptr = reinterpret_cast<const char*>(totalArray);
builder.payload({ ptr, 129 * sizeof(float64) });

mfontanini avatar Aug 07 '18 15:08 mfontanini