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.
Related
I've been inattentive and for some reason created a function that takes two arguments, and I passed it to attachInterrupt like so:
int state = 42;
void simplified_state_handler(){
state++;
}
void interrupt_func(int x, int y) {
simplified_state_handler();
}
attachInterrupt(digitalPinToInterrupt(10), interrupt_func, CHANGE);
The code got compiled with no complaints at all, and it even works. Now, a bit later, I really can't understand why. Reading and digging the attachInterrupt code didn't help. Please explain why can I pass such a function at all. I'm keeping the (now) useless simplified_state_handler in the example, maybe it is important.
The compiler settings of the AVR boards allow it.
It is only a warning: invalid conversion from 'void (*)(int, int)' to 'void (*)()' [-fpermissive].
On other Arduino platforms (SAMD, STM32, esp8266) it is an error.
The compiler settings in AVR platform were benevolent from the start and they can't change them suddenly. Many existing codes would not compile then.
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.
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]));
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.
I have a problem compiling a c++ program with gcc version 4.6.3; I can compile this program with microsoft compiler (v 9.0) without problems.
This program is using some of my libraries I always used with microsoft compiler.
problem is when I try to pass a reference as argument that is a subtype of another: pseudo example here:
class ObjManager{..}
class SubObjMng : public ObjManager{
public:
inline SubObjMng() : ObjManager(0, ... ){}
};
class Test{
public:
Test(int i, ObjManager &obj_mng);
}
int main(){
SubObjMng myobjmng;
Test t(0, myobjmng); //GCC ERROR HERE
}
output of the error is (real output for my program):
globals.h:227:40: error: no matching function for call to cdk::HashMap::HashMap(unsigned int, cdk::PtrObjMng, cdk::cstrObjMng)
globals.h:227:40: note: candidates are:
contrib/cdklib/cdk_struct.h:485:12: note: cdk::HashMap::HashMap(uint32_t, cdk::ObjManager&, cdk::ObjManager&)
contrib/cdklib/cdk_struct.h:485:12: note: no known conversion for argument 2 from cdk::PtrObjMng to cdk::ObjManager&
anyone can help?
thanks!
cdk::PtrObjMng should inherit from cdk::ObjMng, making polymorphism possible through references. Otherwise this is a no-go according to what the compiler says.
Of course this might not be the root of your problem, I wish we could see the implementation of your constructor.