gpt4 book ai didi

c++ - 如何在 header 和 cpp 之间拆分静态/模板类?

转载 作者:搜寻专家 更新时间:2023-10-31 02:02:42 25 4
gpt4 key购买 nike

这是我的静态/模板函数:

template<class T>
static T *createWidget(Vec pos, Module *module, ModuleWidget *moduleWidget, int paramId, float minValue, float maxValue, float defaultValue) {
T *widget = ParamWidget::create<T>(pos, module, paramId, minValue, maxValue, defaultValue);

moduleWidget->mRandomModeWidgets[paramId] = widget;

widget->Module = module;
widget->ModuleWidget = moduleWidget;

return widget;
}

但我想将声明放在 .h 上,将定义放在 .cpp 上。

尝试过:

template<class T>
static T *createWidget(Vec pos, Module *module, ModuleWidget *moduleWidget, int paramId, float minValue, float maxValue, float defaultValue);

比:

template<class T>
static T *MyClasss:createWidget(Vec pos, Module *module, ModuleWidget *moduleWidget, int paramId, float minValue, float maxValue, float defaultValue) {
T *widget = ParamWidget::create<T>(pos, module, paramId, minValue, maxValue, defaultValue);

moduleWidget->mRandomModeWidgets[paramId] = widget;

widget->Module = module;
widget->ModuleWidget = moduleWidget;

return widget;
}

但它说此处可能未指定存储类

我哪里错了?

最佳答案

But it says a storage class may not be specified here.

Where am I wrong?

静态成员函数(模板或非模板)只能在类定义中声明为静态。您正试图在类定义之外声明函数静态。 static 关键字在类定义之外具有不同的含义。只需将其删除:

template<class T>
T *MyClasss::createWidget(params...) {
^ ^^ alśo note that there must be two colons in the scope resolution operator
\ no static

还要记住,在任何翻译单元中使用的模板实例必须在定义该模板的翻译单元中实例化。这可以通过在单独的 cpp 文件中显式实例化来实现。

关于c++ - 如何在 header 和 cpp 之间拆分静态/模板类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56867720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com