I have inherited some C++ code that sets up a serial com port in Windows. The code contains the following declaration and method, which I have simplified for the purposes of this question.
std::queue<std::string> m_queue;
SendMsg( const char* dataBuff, unsigned int dataBuffSz )
{
// Insert into the end of the queue to send
m_queue.emplace( dataBuff, dataBuffSz );
}
If I create a suitable object to invoke SendMsg then the following is valid and is successfully transmitted across the serial link
mySerialLinkObject->SendMsg( "Hello", 5 );
// or alternatively
char hexBytes[4] = { 0x01, 0x02, 0x0F, 0x7F };
mySerialLinkObject->SendMsg( hexBytes, 4 );
My specific requirements for this link are for it to be able to send hex bytes in the range 0x00 to 0xFF however if I declare
char hexBytes[2] = { 0x80, 0xFF };
In Visual Studio 2015 I get the following build output from the declaration of hexBytes line above
error C2220: warning treated as error - no 'object' file generated warning C4838: conversion from 'int' to 'char' requires a narrowing conversion note: to simplify migration, consider the temporary use of /Wv:18 flag with the version of the compiler with which you used to build without warnings warning C4309: 'initializing': truncation of constant value
If I change my hexBytes definition as follows
uint8_t hexBytes[2] = { 0x80, 0xFF }; //unsigned char fixed width
This fixes the initial compiler error and if I then overload the SendMsg method to
SendMsg( const uint8_t* dataBuff, unsigned int dataBuffSz )
{
// Insert into the end of the queue to send
m_queue.emplace( dataBuff, dataBuffSz );
}
Then I get a whole load of std::queue related compiler errors
xmemory0(737): error C2664: 'std::basic_string<char,std::char_traits,std::allocator>::basic_string(std::initializer_list<_Elem>,const std::allocator &)': cannot convert argument 1 from 'const uint8_t *' to 'const std::basic_string<char,std::char_traits,std::allocator> &' 1> with 1> [
Its not my code so I don't really understand what it did initially when it placed the char* parameter in to a queue of std::string and why I am not allowed by the compiler to place "unsigned char*" in to the std::string queue.
Im not sure what the best way to fix this.
Do I fix the initial C4838 error when declaring hexBytes by doing some form of conversion, In which case I am not sure how?
Or do I somehow try to fix the std::queue errors I am getting. Again I'm not sure of the best way to achieve that