I'm trying to use addImageAsync for the first time, but i cannot get the syntax working. I'm using Cocos2dx 3.3 final and Xcode 6.1.1.
My code is as follows :
(GFXManager.h)
#include "cocos2d.h"
class GFXManager
{
...
void loadStuff();
void textureLoaded(Ref* pObj);
}
(GFXManager.cpp)
...
void GFXManager::loadStuff()
{
std::string path = "whatever.png";
Director::getInstance()->getTextureCache()->addImageAsync(path, callfuncO_selector(GFXManager::textureLoaded));
}
void GFXManager::textureLoaded(Ref* pObj)
{
...
}
The above is based on the "Texture2dTest" sample from Cocos2dx.
But at the line with the addImageAsync instruction Xcode keeps saying this:
Static_cast from 'void (GFXManager::*)(cocos2d::Ref * )' to 'cocos2d::SEL_CallFuncO' (aka 'void (cocos2d::Ref::*)(cocos2d::Ref *)') is not allowed
I tried making 'GFXManager' a class derived from 'Layer' (as in Texture2dTest), and using 'CCObject' in place of 'Ref' (as in Texture2dTest...But 'CCObject' is deprecated and now it is called 'Ref'), with no luck.
But every example i've found on the web about using addImageAsync calls the selector with that syntax.
So what am i doing wrong ?
You need to change callfuncO_selector with std::bind or CC_CALLBACK_1:
void GFXManager::loadStuff()
{
std::string path = "whatever.png";
Director::getInstance()->getTextureCache()->addImageAsync(path, CC_CALLBACK_1(GFXManager::textureLoaded, this));
}
because TextureCache::addImageAsync accepts std::function not the function pointer
Related
I have rather a basic knowledge in c++ and there is an issue described in the following which is likely rather simple and a based on a syntax mistake but I haven't figured out a way around it.
Basically the error I get says:
remote_hw_interface_node.cpp:23:68: required from here
/usr/include/boost/function/function_template.hpp:231:11: error: no match for call to ‘(boost::_mfi::mf1<void, ROBOTHardwareInterface, const boost::shared_ptr<sensor_msgs::JointState_<std::allocator<void>
> > >&>) (const boost::shared_ptr<const sensor_msgs::JointState_<std::allocator<void> > >&)’
BOOST_FUNCTION_RETURN(boost::mem_fn(*f)(BOOST_FUNCTION_ARGS));
^
which I don't have any clue what it is about regarding boost.
regarding my code I have copied some parts of it in the following which may likely show the problem for a more experienced c++ user. My header file looks like:
#pragma once
#include <message_filters/subscriber.h>
#include <message_filters/time_sequencer.h>
// controller manager and interface msgs
#include <controller_manager/controller_manager.h>
class ROBOTHardwareInterface : public hardware_interface::RobotHW
{
public:
ROBOTHardwareInterface(ros::NodeHandle& nh);
~ROBOTHardwareInterface();
bool init (const urdf::Model* const urdf_model);
void sequential_update (const boost::shared_ptr <sensor_msgs::JointState> & joint_state_msg);
// main
ros::NodeHandle nh_;
ros::Duration elapsed_time_;
boost::shared_ptr<controller_manager::ControllerManager> controller_manager_;
};
the cpp file also if I only copy the relevant parts which has also caused the error coming up:
#include <remote_hw.h>
ROBOTHardwareInterface::ROBOTHardwareInterface(ros::NodeHandle& nh) : nh_(nh) {
message_filters::Subscriber <sensor_msgs::JointState> sub(nh_, "joint_cmd_topic", 1);
message_filters::TimeSequencer <sensor_msgs::JointState> seq(sub, ros::Duration(
0.1), ros::Duration(0.01), 10);
seq.registerCallback(&ROBOTHardwareInterface::sequential_update);
}
ROBOTHardwareInterface::~ROBOTHardwareInterface() {
}}
void ROBOTHardwareInterface::sequential_update(const boost::shared_ptr <sensor_msgs::JointState> & joint_state_msg){
By searching in boost library implementation https://www.boost.org/doc/libs/1_71_0/boost/function/function_template.hpp, it seems that you are trying to call a method without giving all the parameters necessary.
I suppose that you try to invoke this method with an empty parameter, while you should also add const boost::shared_ptr <sensor_msgs::JointState> & as well.
I suppose that BOOST_FUNCTION_ARGS in your case is empty hence your issue. So the method behind is trying to invoke void sequential_update() instead of void sequential_update(const boost::shared_ptr <sensor_msgs::JointState> &)
I hope this helps.
I just started learning C++ for an Arduino project me and a friend have been working on. I'm getting the error "No matching function for call to 'QuadBase::QuadBase'" in main.cpp. I'not sure what is causing it, since I have the correct amount of arguments and they are the same type as well
edit: I brought it down to this and it still gives me that same error
#include <Arduino.h>
#include "QuadBase.h"
QuadBase base;
void setup()
{
base = QuadBase(
...
);
}
QuadBase.h
class QuadBase
{ public:
QuadBase( ... )
{
...
}
};
It seems your class QuadBase is missing a default constructor (one that takes no arguments) which is needed for the line
QuadBase base;
I am working on plugin system using Boost.dll
#include <boost/config.hpp>
#include <boost/dll/alias.hpp>
#include <memory>
class base {
public:
base(){};
~base(){};
template <typename T>
static std::shared_ptr<base> create() {
return std::make_shared<T>();
}
virtual void do1() = 0;
};
class derived : public base {
public:
derived(){};
~derived(){};
virtual void do1() override {}
};
BOOST_DLL_ALIAS(base::create<derived>, // <-- this function is exported with...
create_plugin // <-- ...this alias name
)
// auto a = base::create<derived>();
When I trying use factory method inside BOOST_DLL_ALIAS macro getting error like below.
cl /c /ID:\Download\Compressed\boost_1_67_0 a.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.12.25834 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
a.cpp
a.cpp(27): error C2440: 'reinterpret_cast': cannot convert from 'overloaded-function' to 'intptr_t'
a.cpp(27): note: Context does not allow for disambiguation of overloaded function
Without BOOST_DLL_ALIAS macro when calling factory method (as shown in last commented line), it get compiled fine.
// this really does nothing:
template<class R, class...Args>
R(*to_fptr( R(*f)(Args...) ))(Args...) {
return f;
}
// except avoid the bug in MSVC:
BOOST_DLL_ALIAS(to_fptr(base::create<derived>),
create_plugin
)
that should fix it.
MSVC seems to break when you try to cast from the name of a template function to a intptr_t. This is a bug.
The above workaround changes it from directly dealing with the name of a template function to just a function pointer. By breaking the overload resolution and the cast to intptr_t apart, MSVC no longer chokes.
You could probably also do:
template<class T>
T identity( T t ) { return std::move(t); }
instead of to_fptr.
to_fptr is a function that takes a function pointer and returns it. The syntax to return a function pointer from a function is a bit ridiculous, which is why it is hard to read.
Inspired by this tangentially related answer, I found another solution:
const void * create_plugin =
reinterpret_cast<const void*>(
reinterpret_cast<std::intptr_t>(
reinterpret_cast<std::decay_t<decltype(&base::create<derived>)> >(
&base::create<derived>
)
)
);
https://godbolt.org/z/NVGz_0
This should do the trick:
BOOST_DLL_ALIAS(std::function<std::shared_ptr<base>(void)>(base::create<derived>), // <-- this function is exported with...
create_plugin // <-- ...this alias name
)
Indeed it seems MSVC has a bug here, as there should be no problem taking such an expression base::create<derived> and treating it directly as a function pointer. So instead, we "feed it a function pointer" ourselves using the magic of std::function.
I've chosen to use std::function as it involves no writing of any kind of wrapping code and relies directly on intent-delivering standard c++ code.
And of course don't forget to #include <functional> : )
I am trying to move my FLTK projects and compile it under the VS 2015 community edition. While do this, I am getting error. I have a code like below:
#include <Fl/....>
....
class CWindow
{
private:
....
Fl_Input *_textInputEditor;
....
void _cbTextInput(Fl_Widget *refObject, void *objData)
{
// Do something when callback is triggered.
}
public:
....
void createWindow()
{
....
_textInputEditor = new Fl_Input(....);
_textInputEditor->when(FL_WHEN_ENTER_KEY);
_textInputEditor->callback((Fl_Callback*)&CWindow::_cbTextInput, this);
....
When I try to compile, I get an error:
Error C2440 'type cast': cannot convert from 'void (__thiscall CWindow::* )(Fl_Widget *,void *)' to 'Fl_Callback (__cdecl *)
This same code compiles with MinGW 5.x perfectly (IDE: C::B) under Win 7.
Can someone help me here? I want a call back to a private method of my CWindow class.
The signature is incorrect. _cbTextInput should be static. The problem then will be access to member variables.
static void _cbTextInput(Fl_Widget *refObject, void *objData)
{
// No access to member variables so cast it to a CWindow* to get
// at the member variables
CWindow* self = (CWindow*) objData;
self->cbTextInput();
// Alternatively, if you do not wish to write another routine,
// you will need to put self-> in front of every member variable
// you wish to access. Just personal preference: some people prefer
// it that way.
}
void cbTextInput()
{
// This has access to member variables
...
}
I am having trouble passing a template class as a parameter to a another function.
I am using VS2012 c++/cli on a Windows 8.1 machine compiling for x64.
The compiler keeps telling me:
void Channel::TestFunc(SynchQueue<T> *)' : overloaded member function not found in 'Channel'
SynchQueue is a template class for a multi-threaded queue. I created it with another class I will call Images.
In my main.cpp, I have:
QPtr = new SynchQueue<Images>;
Also in main.cpp, I created a class called WorkerThread to which I passed QPtr.
No problems with that.
Now I want WorkerThread to pass QPtr to another class that is instantiated in WorkerThread.
So I defined the function as:
Channel.h
public ref class Channel
{
public:
// other definition stuff
void TestFunc(SynchQueue<Images> *tQPtr);
}
Channel.cpp
void Channel::TestFunc(SynchQueue<Images> *tQPtr)
{
int x;
x++;
}
I keep getting the error above. What am I doing wrong?
Any help appreciated.
You need:
template<typename T>
void TestFunc(SynchQueue<T> *tQPtr);