C++ 如何在Coco2d-x中的CREATE_FUNC中指定参数
我的C++ 如何在Coco2d-x中的CREATE_FUNC中指定参数,c++,ios,cocos2d-x,C++,Ios,Cocos2d X,我的messagebox类在构造函数中传递了一个参数,我希望知道如何在CREATE\u FUNC()中指定参数,在本上下文中是std::string\u content 我得到的错误是“构造函数不能声明为‘static’” 这是MessageBox的代码。h: class MessageBoxes : public cocos2d::Node { private: Sprite* _sprite; bool _speaking; float
messagebox
类在构造函数中传递了一个参数,我希望知道如何在CREATE\u FUNC()
中指定参数,在本上下文中是std::string\u content
我得到的错误是“构造函数不能声明为‘static’”
这是MessageBox的代码。h:
class MessageBoxes : public cocos2d::Node
{
private:
Sprite* _sprite;
bool _speaking;
float _speakingTime;
std::string _content;
public:
CREATE_FUNC(MessageBoxes(std::string content));
protected:
virtual bool init(std::string _content);
void setSprite();
void setContent();
};
CREATE_FUNC是在CCPlatformMacros.h中定义的预定义宏
#define CREATE_FUNC(__TYPE__) \
static __TYPE__* create() \
{ \
__TYPE__ *pRet = new(std::nothrow) __TYPE__(); \
if (pRet && pRet->init()) \
{ \
pRet->autorelease(); \
return pRet; \
} \
else \
{ \
delete pRet; \
pRet = NULL; \
return NULL; \
} \
}
代码
创建_FUNC(消息框(std::string content))
实际上是
new(std::nothrow) MessageBoxes(std::string content)();
在C++中有编译错误。 但是您可以自己编写类似于create_FUNC的create函数,如
static MessageBoxes* create(std::string content) {
MessageBoxes* ret = new(std::nothrow) MessageBoxes();
if(ret && ret->init(content)) { //<----Or anything you wanna init with
ret->autorelease();
return ret;
} else {
delete ret;
ret = nullptr;
return nullptr;
}
}
静态消息框*创建(std::字符串内容){
MessageBox*ret=new(std::nothrow)MessageBox();
if(ret&&ret->init(content)){//autorelease();
返回ret;
}否则{
删除ret;
ret=空PTR;
返回空ptr;
}
}
@VictorPolevoy感谢您的提醒…修复!问题中更新的CREATE\u FUNC(
行?@VictorPolevoy)有哪些错误?不要将其放入代码段中,按照编译器在代码段下所说的方式编写。请查看错误消息是如何编写的,并尝试执行相同的操作。谢谢,是的,您提供的代码中有一些小错误