C++ threading - no matching function for call to - c++

This is my first time using threads in c++ and I have some issues with it. I am getting error
error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, __gnu_cxx::__alloc_traits<std::allocator<packetInfo> >::value_type*)’
This is my code excerpt:
std::vector<packetInfo> sentPackets; // global var
void renewIP(struct packetInfo *currentPacket) {
...//code
}
void anotherFuntion() {
...
std::thread renewTimer(renewIP, &(sentPackets[i]));
renewTimer.detach();
...
}
I have absolutely no idea what am I doing wrong or why am I getting the error.
Thanks.

The error tells you directly:
<unresolved overloaded function type>
You must have multiple overloads of renewIP, and the compiler doesn't know which one you want. You can either rename them to make them not ambiguous, or make it explicit via a cast:
std::thread renewTimer((void(*)(struct packetInfo*))renewIP, &(sentPackets[i]));

Related

Ambigious call to overloaded function when using lambda

Never used lambdas before and I can't understand where I'd have to add it.
My Error is "Show: Ambigious call to overloaded function"
Show() can take 2 types CustomizeToast and CustomizeToastAsync. So I guess I need to specify CustomizeToast somewhere but I can't for the life of me see where.
This is my current code:
ToastContentBuilder()
.AddText(L"Hello World!")
.Show([](ToastNotification const& toast) -> void
{
toast.Tag(L"1");
});
Error:
error C2668: 'winrt::impl::consume_Microsoft_Toolkit_Uwp_Notifications_IToastContentBuilderClass<winrt::Microsoft::Toolkit::Uwp::Notifications::IToastContentBuilderClass>::Show': ambiguous call to overloaded function
Microsoft.Toolkit.Uwp.Notifications.0.h(2159,60): message : could be 'auto winrt::impl::consume_Microsoft_Toolkit_Uwp_Notifications_IToastContentBuilderClass<winrt::Microsoft::Toolkit::Uwp::Notifications::IToastContentBuilderClass>::Show(const winrt::Microsoft::Toolkit::Uwp::Notifications::CustomizeToastAsync &) const'
Microsoft.Toolkit.Uwp.Notifications.0.h(2158,31): message : or 'auto winrt::impl::consume_Microsoft_Toolkit_Uwp_Notifications_IToastContentBuilderClass<winrt::Microsoft::Toolkit::Uwp::Notifications::IToastContentBuilderClass>::Show(const winrt::Microsoft::Toolkit::Uwp::Notifications::CustomizeToast &) const'
message : while trying to match the argument list '(winrt::MyApplication::implementation::Toast::ShowToastNotification::<lambda_786678859ea03b85c00686eebdcb39db>)'
Project is an UWP project using the Microsoft.Toolkit.Uwp.Notifications
Show() can take 2 types CustomizeToast and CustomizeToastAsync.
This is clearly an oversight on the API's developpers end. Now since both classes can be constructed from a lambda the compiler doesn't know which one to use, so you have to guide it:
ToastContentBuilder()
.AddText(L"Hello World!")
.Show(CustomizeToast{[](ToastNotification const& toast) -> void
{
toast.Tag(L"1");
}});
This is less readable and cumbersome, but constructing syntaxic suggar for it would be error-prone and a bit convoluted.

Can unity C unittest framework be used to unittest C++ code?

I'm using the unity framework to unittest my C code on an AtMega32A. This works great. now I wonder if I can somehow trick unity to also test my C++ code.
I've made a small proof of concept program that exposes all the members of the C++ class via a public function. Now I would like to put a number of assert macro's in that public function. The program can be found here:
https://github.com/cdwijs/cpp_unit_test
In my main I have the following:
int main(void)
{
UNITY_BEGIN();
//RUN_TEST(myPrivate.executeTest); //unity_internals.h(707,65): error: invalid use of non-static member function
//RUN_TEST((void*)myPrivate.executeTest); //error: invalid use of member function (did you forget the '()' ?)
//RUN_TEST((func)))myPrivate.executeTest); //error: 'func' was not declared in this scope
//RUN_TEST(&myPrivate.executeTest); //error: cannot convert 'void (Private::*)()' to 'UnityTestFunction {aka void (*)()}' for argument '1' to 'void UnityDefaultTestRun(UnityTestFunction, const char*, int)'
UNITY_END();
myPrivate.more();
myPrivate.more();
myPrivate.less();
/* Replace with your application code */
while (1)
{
}
}
And in Private_test.cpp I have the following:
#include "private.h"
#include "unity.h"
void Private::executeTest (void)
{
myNumber = 3;
TEST_ASSERT_EQUAL_INT(3,myNumber);
more();
TEST_ASSERT_EQUAL_INT(4,myNumber);
}
I can't figure out howto run the test function. Any Idea's? Am I looking in the correct direction, or should I use a test framework that is specifically aimed at C++?

C++ pass reference to function inside same class

I am developing an embedded system with the mbed framework in C++.
To attach an interrupt function to the serial interrupt, I normally do this:
Serial pc(pin_u_tx, pin_u_rx,115200);
void SerialStart(void) {
...
pc.attach(&SerInt);
...
}
void SerInt(){
...
}
But now I need to do the same thing from inside a class, and it doesn't work as I can't refer to an internal function:
CTCOMM::CTCOMM()
{
pc = new Serial(ser_tx, ser_rx, ser_baud);
pc->attach(&serial_interrupt);
}
void CTCOMM::serial_interrupt() {
...
}
I tried a few ways, but none works:
pc->attach(&serial_interrupt);
gives the error
lib\CTcomm\ctcomm.cpp:12:17: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&CTCOMM::serial_interrupt' [-fpermissive]
pc->attach(*serial_interrupt);
gives the error
lib\CTcomm\ctcomm.cpp:12:17: error: invalid use of member function 'void CTCOMM::serial_interrupt()' (did you forget the '
pc->attach(*serial_interrupt());
gives the error
lib\CTcomm\ctcomm.cpp:12:33: error: void value not ignored as it ought to be ()' ?)
pc->attach((*this)->*(serial_interrupt));
gives the error
lib\CTcomm\ctcomm.cpp:12:23: error: invalid use of non-static member function 'void CTCOMM::serial_interrupt()'
and so on (I tried more suggestions found here, but got no succes).
What would be the correct way to point to that function?
Try this.
pc->attach(callback(this, &CTCOMM::serial_interrupt));
pc->attach(this, &CTCOMM::serial_interrupt); should also work. But it is deprecated in the recent versions of mbed OS.
Here is the latest Mbed API:
https://os.mbed.com/docs/v5.10/mbed-os-api-doxy/classmbed_1_1_serial.html

Eclipse giving ambigous function warning on overloaded function

I have these two functions
bool checkIfPlayerWon(const F &field){
return checkIfPlayerWon(field, player);
}
bool checkIfPlayerWon(const F &field, int current_player){
player is a member var of my class.
Now in my main I call
if(human_player.help.checkIfPlayerWon(my_field))
and
if(pc_player.help.checkIfPlayerWon(my_field))
Now everything compiles fine and the program works as intended. But eclipse gives me warnings, that the function checkIfPlayerWon is ambigous and then shows me:
'checkIfPlayerWon' is ambiguous '
Candidates are:
bool checkIfPlayerWon(const playfield &)
bool checkIfPlayerWon(const playfield &, int)
'
Why does eclipse do this? It's clear that it should call the first function, because it only has one argument. The gcc compiler gets it but why not eclipse?
I don't know how to make Eclipse figure out which overloaded function you want, but the ambiguous warning is decently easy to turn off without disabling the entire C++ parser.
Go to Window->Preferences->C/C++->Cody Analysis and write ambiguous in the search tab. Then it will let you toggle the warning off.

error: expected primary-expression before ')' token (C)

I am trying to call a function named characterSelection(SDL_Surface *screen, struct SelectionneNonSelectionne sel) which returns a void
This is the .h of the function I try to call:
struct SelectionneNonSelectionne;
void characterSelection(SDL_Surface *screen, struct SelectionneNonSelectionne);
void resetSelection(SDL_Surface *screen, struct SelectionneNonSelectionne);
On my main function, I try to call it like this:
characterSelection(screen, SelectionneNonSelectionne);
When I compile, I have the message:
error: expected primary-expression before ')' token
I made the includes. I suppose I miscall the second argument, my struct. But, I can't find why on the net.
Have you got any idea about what I did wrong?
You should create a variable of the type SelectionneNonSelectionne.
struct SelectionneNonSelectionne var;
After that pass that variable to the function like
characterSelection(screen, var);
The error is caused since you are passing the type name SelectionneNonSelectionne
A function call needs to be performed with objects. You are doing the equivalent of this:
// function declaration/definition
void foo(int) {}
// function call
foo(int); // wat!??
i.e. passing a type where an object is required. This makes no sense in C or C++. You need to be doing
int i = 42;
foo(i);
or
foo(42);
You're passing a type as an argument, not an object. You need to do characterSelection(screen, test); where test is of type SelectionneNonSelectionne.
I seen this problem with the latest nightly build of Code::Blocks. When I switched back to the stable release of Code::Blocks, 20.03 at the time of this writing, the problem went away and my code compiled and ran without problems. I'm not sure what Code::Blocks is doing, but it is very annoying. I got this repeatedly on a C++ project for every NULL in my code, forcing me to use nullptr instead.