Arduino, understanding invalid conversion warning, unsigned char to char* - c++

Edit:
Resolved, can't accept my answer for two days
I'm using PlatformIO to program a Arduino Nano to write a message to an OLED display.
I'm calling a function printWeight:
void printWeight(
float weight,
uint8_t scale=3,
uint8_t numbd=3,
uint8_t numad=2,
bool kg=false,
const char* description_text_line="Weight:",
uint8_t width_per_character=5+1,
uint8_t height_per_line=7+3
){
...
The function is centering the text by calculating the leading space leading_space and calls itself with a smaller scale (scale-1) if the message doesn't fit on the screen:
if(leading_space < 0){
printWeight(weight, scale-1, numbd, numad, description_text_line,
width_per_character, height_per_line);
return;
}
The code compiles and runs fine. However I'm getting the following warning that I don't know how to resolve:
src/main.cpp: In function 'void printWeight(float, uint8_t, uint8_t, uint8_t, bool, const char*, uint8_t, uint8_t)':
src/main.cpp:138:53: warning: invalid conversion from 'uint8_t {aka unsigned char}' to 'const char*' [-fpermissive]
width_per_character, height_per_line);
^
src/main.cpp:93:6: note: initializing argument 6 of 'void printWeight(float, uint8_t, uint8_t, uint8_t, bool, const char*, uint8_t, uint8_t)'
void printWeight(
^
How do I get rid of this warning?

Missing parameter...
Thanks to the ones commenting.

Related

callback in MCS6502 functions will not compile in arduino on esp32 but will compile in codeblocks c file

I know this is a noob question bit I am not familiar with c++ .
My understanding is that arduino code compiles in c++ and this is what causes the problem in compiling.
The code below will compile fine in codeblocks and will run correctly , however if I use the same code without any modifications then it will produce an error at compile time.
heres is the .ino file
#include "MCS6502.h"
int8_t ram [65535];
//////CALL BACK FUNCTION ///////////////////////////////////////////////
uint8_t readBytesFunction(uint16_t add) {
uint8_t tc = 5;
tc = ram[add];
return tc;
}
//////CALL BACK FUNCTION ///////////////////////////////////////////////
void writeBytesFunction(uint16_t add,uint8_t bb) {
}
void setup()
{
Serial.begin(115200);
Serial.println();
///CODE BELOW WILL COMPILE AND RUN IN CODEBLOCKS BUT WILL NOT COMPILE IN ARDUINO
/////////////////////////////////////////////////////////////////////
MCS6502ExecutionContext context;
MCS6502Init(&context, readBytesFunction, writeBytesFunction, NULL); // Final param is optional conte>
MCS6502Reset(&context);
MCS6502Tick(&context); //use timings
MCS6502ExecNext(&context); //as fast as possible
}
these are the errors after compiling in arduino for esp32
MCS6502.ino: In function 'void setup()':
wahid_MCS6502:22:27: error: invalid conversion from 'uint8_t (*)(uint16_t)' {aka 'unsigned char (*)(short unsigned int)'} to 'MCS6502DataReadByteFunction' {aka 'unsigned char (*)(short unsigned int, void*)'} [-fpermissive]
MCS6502Init(&context, readBytesFunction, writeBytesFunction, NULL); // Final param is optional conte>
^~~~~~~~~~~~~~~~~
MCS6502.h:91:33: note: initializing argument 2 of 'void MCS6502Init(MCS6502ExecutionContext*, MCS6502DataReadByteFunction, MCS6502DataWriteByteFunction, void*)'
MCS6502DataReadByteFunction readByteFn,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
MCS6502:22:46: error: invalid conversion from 'void (*)(uint16_t, uint8_t)' {aka 'void (*)(short unsigned int, unsigned char)'} to 'MCS6502DataWriteByteFunction' {aka 'void (*)(short unsigned int, unsigned char, void*)'} [-fpermissive]
MCS6502Init(&context, readBytesFunction, writeBytesFunction, NULL); // Final param is optional conte>
^~~~~~~~~~~~~~~~~~
MCS6502.h:92:34: note: initializing argument 3 of 'void MCS6502Init(MCS6502ExecutionContext*, MCS6502DataReadByteFunction, MCS6502DataWriteByteFunction, void*)'
MCS6502DataWriteByteFunction writeByteFn,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
exit status 1
invalid conversion from 'uint8_t (*)(uint16_t)' {aka 'unsigned char (*)(short unsigned int)'} to 'MCS6502DataReadByteFunction' {aka 'unsigned char (*)(short unsigned int, void*)'} [-fpermissive]
what do i need to do it compiles in arduino ??
apologies if some details are missing from this question but its my 1st ever time asking a question here.
many thanks
I'm guessing that you are using this library?
Looking at the signatures of the callback functions both require a final void* parameter which your callbacks are missing.
You need:
uint8_t readBytesFunction(uint16_t add, void*) {
uint8_t tc = 5;
tc = ram[add];
return tc;
}
void writeBytesFunction(uint16_t add,uint8_t bb, void*) {
}

cpp newbie question: error: conversion to ‘std::array<int, 5>::size_type {aka long unsigned int}’ from ‘int’ may change the sign of the result

I'm trying to implement my own swap function for array, but it seems to have conversion problem.
And I do not have a clue.
Forgive me if this question is too stupid.
Here is the source code:
x_quiz3.h
#include<array>
using namespace std;
void swap_arr_element(array<int,5> &arr, int index1, int index2){
int temp{arr.at(index1)};
arr.at(index1) = arr.at(index2);
arr.at(index2) = temp;
}
void start_x_quiz3(){
array arr{1,2,4,3,5};
swap_arr_element(arr, 2, 3);
for (auto &i : arr){
cout << i << " ";
}
}
main.cpp:
#include <iostream>
#include "x_quiz3.h"
int main()
{
start_x_quiz3();
return 0;
}
error message:
||=== Build: Debug in chapter6 (compiler: GNU GCC Compiler) ===|
/home/lewisluk/CodeBlocksProjects/tutorials/quizs/chapter6/x_quiz3.h||In function ‘void swap_arr_element(std::array<int, 5>&, int, int)’:|
/home/lewisluk/CodeBlocksProjects/tutorials/quizs/chapter6/x_quiz3.h|8|error: conversion to ‘std::array<int, 5>::size_type {aka long unsigned int}’ from ‘int’ may change the sign of the result [-Werror=sign-conversion]|
/home/lewisluk/CodeBlocksProjects/tutorials/quizs/chapter6/x_quiz3.h|9|error: conversion to ‘std::array<int, 5>::size_type {aka long unsigned int}’ from ‘int’ may change the sign of the result [-Werror=sign-conversion]|
/home/lewisluk/CodeBlocksProjects/tutorials/quizs/chapter6/x_quiz3.h|9|error: conversion to ‘std::array<int, 5>::size_type {aka long unsigned int}’ from ‘int’ may change the sign of the result [-Werror=sign-conversion]|
/home/lewisluk/CodeBlocksProjects/tutorials/quizs/chapter6/x_quiz3.h|10|error: conversion to ‘std::array<int, 5>::size_type {aka long unsigned int}’ from ‘int’ may change the sign of the result [-Werror=sign-conversion]|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
The argument to the at function is size_type, which is an unsigned type.
The index arguments to your swap function are int which is a signed type.
The compiler says that it can not safely convert from a signed type to an unsigned type (which it can't, think about what happens if you provide negative indexes).
The simple solution is to make the swap function index arguments unsigned as well, preferably using the size_t type:
void swap_arr_element(array<int,5> &arr, size_t index1, size_t index2){ ... }

How to fix: cannot convert 'uint8_t (*)[50] {aka unsigned char (*)[50]}' to 'uint8_t* {aka unsigned char*}'

Im programming ATMEGA328p for a specific problema. Im using libraries in C and C++.
When I compile the code the present erro apears:
error: cannot convert 'uint8_t ()[50] {aka unsigned char ()[50]}' to 'uint8_t* {aka unsigned char*}' for argument '2' to 'void SendXbee(char, uint8_t*)
My call to function is:
uint8_t tempBuffer[50]
SendXbee(_data, &tempBuffer);
In library headfile:
void SendXbee(char Data, uint8_t *Frame);
In .c archive the function is:
void SendXbee(char Data, uint8_t* Frame){
.
.
.
xBeeAPI_To_Buffer_From_Frame(&Frame, &transmitFrame);
}
What im doing wrong?

error: invalid conversion from 'void*' to 'const uint8_t* {aka const unsigned char*}' [-fpermissive]

I am trying to import a C library into my C++ project but I'm stuck with this error
invalid conversion from 'void*' to 'const uint8_t* {aka const unsigned char*}' [-fpermissive]
uint8_t const* raw = static_cast(getPointerToData(id, message->data, message->length));
^
compilation terminated due to -Wfatal-errors.
This code compile well using C compiler but get this error using C++ one
bool XbusMessage_getDataItem(void* item, enum XsDataIdentifier id, struct XbusMessage const* message)
{
uint8_t const* raw = (getPointerToData(id, message->data, message->length));
Can you help me?
Thanks
You need to explicitly cast the return value from getPointerToData to const uint8_t*. Implicit conversions between pointer types are not allowed in C++ but are in C.
Try:
uint8_t const* raw = static_cast<uint8_t const*>(getPointerToData(id, message->data, message->length));
Edit:
You can also use a C-style cast if you want to keep the C code C and not C++.
uint8_t const* raw = (uint8_t const*)(getPointerToData(id, message->data, message->length));
This code posted by Jean-François Fabre works
uint8_t const* raw = (getPointerToData(id, static_cast<const uint8_t*>(message->data), message->length));
Thanks!

c++: error: cannot convert ‘ns3::TracedValue<ns3::SequenceNumber<unsigned int, int> >’ to ‘uint32_t {aka unsigned int}

I am trying to cast TracedValue<uint32_t> m_bytesInFlight to uint32_t but I get the following error
error: cannot convert ‘ns3::TracedValue<ns3::SequenceNumber<unsigned int, int> >’ to ‘uint32_t {aka unsigned int}
Function prototype and variable declarations are
uint32_t UnAckDCount (void) const;
TracedValue<uint32_t> m_bytesInFlight {0}; //!< Bytes in flight
Here i am calling the function
uint32_t
TcpSocketBase::UnAckDCount () const
{
return m_tcb->m_highTxMark - (uint32_t) m_tcb->m_bytesInFlight;
}
Please suggest some method so that I can execute the return statement to get the result. Thanks in advance
Changing m_tcb->m_highTxMark to m_tcb->m_highTxMark.Get().GetValue() should work.
Seeing the compiler error, it's easy to figure out that variable m_highTxMark is of type ns3::TracedValue<ns3::SequenceNumber<unsigned int, int> >. I checked the documentation of ns3::TracedValue and ns3::SequenceNumber, they both have getter functions Get() and GetValue() respectively.
See:
https://www.nsnam.org/doxygen/classns3_1_1_traced_value.html
https://www.nsnam.org/doxygen/classns3_1_1_sequence_number.html