1

This is the static/template function I have:

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;
}

But I'd like to place the declaration on .h and the definition on .cpp.

Tried:

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

And than:

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?

markzzz
  • 47,390
  • 120
  • 299
  • 507
  • @YanB. all definitions are declarations. And also, templates can be forward declared as well. – eerorika Jul 03 '19 at 10:16
  • Possible duplicate of [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – L. F. Jul 03 '19 at 10:16
  • @eerorika You're right, I meant declarations with no definitions like those with normal functions, which can be in a separate file from a definition. Terminology... –  Jul 03 '19 at 10:18
  • @YanB. that's what forward declaration is, and as I said; templates can be forward declared. – eerorika Jul 03 '19 at 10:18
  • @YanB. That's just wrong. `template struct X;` and `template void foo();` are class and function template declarations, respectively. In common usage it may not make sense to provide declaration and definition in separate files, but that doesn't mean those concepts do not exist. And you totally can have a template declared in the `.h` and defined in the `.cpp`, which is practical when combined with explicit template instantiations (and some other specific scenarios). – Max Langhof Jul 03 '19 at 10:52
  • Regarding the question: When the comiler says "don't do X right here" then don't do X right there and see what happens. Would've solved the problem in this case. – Max Langhof Jul 03 '19 at 10:57

1 Answers1

1

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

Where am I wrong?

Static member function (template or not) may only be declared static within the class definition. You're attempting to declare the function static outside the class definition. static keyword has a different meaning outside the class definition. Simply remove it:

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

Also remember that template instances that are used in any translation unit, must be instantiated in a translation unit where that template is defined. This can be achieved with explicit instantiation within that separate cpp file.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • But I want to call that function using `MyClass::createWidget()` without any `MyClass` instance... – markzzz Jul 03 '19 at 10:38
  • @markzzz you can so that. Simply declare the function static in the class definition. – eerorika Jul 03 '19 at 10:41
  • If I do it, once I include the header with that class (without the method declaration) it simply fail: can't call that method from a comp-unit that include that file (and not the definition). – markzzz Jul 03 '19 at 12:21
  • @markzzz you must declare all member functions in the class definition. – eerorika Jul 03 '19 at 12:28
  • Are you saying do to `T *MyClasss::createWidget(params...);` in header file, while define it with static in the source file? :O – markzzz Jul 03 '19 at 12:39
  • @markzzz No. Declare in the header file with `static` within the class. Define without static, when defining outside the class definition, whether in a source file or a header. – eerorika Jul 03 '19 at 12:40