1

I have a C++ class definition declared in a header file as follows:

template <class T>
class MyClass : public T
{

public:

    STDMETHODIMP myMethod();

};

The implementation in a .cpp is this:

template <class T>
STDMETHODIMP MyClass<T>::myMethod() {
// Implementation...
}

The compiler (Visual Studio) is complaining about the method implementation, saying that "anachronism used : modifiers on data ignored" and also "unrecognizable template declaration / definition".

Any ideas what's going wrong?

EDIT:

Might the error be here?

I am trying to extend MyClass, so I declare

class ChildClass : MyClass<SomeConcreteClass>
{
// Stuff...
};
1
  • If you want to keep your headers clean, you can write it into a .inl file and simply include "<implementation>.inl" at the end of your header Commented Dec 5, 2014 at 15:16

1 Answer 1

0
  1. The definition of the template function has to be in the header file too. (The only exception to this rule is if the template is only instantiated in one compilation unit; in which case you can put the definitions in the same source.). So move the implementation out of the .cpp and into the header.

  2. Your writing STDMETHODIMP in the class declaration looks suspect as well (and is probably the source of the immediate error). Are you sure there's not another macro like STDMETHOD that you should be using there?

Sign up to request clarification or add additional context in comments.

4 Comments

that isn't the only exception, you can also explicitly instantiate in the header and then define the implementation in the cpp file. But that's like, a super edge case that no one ever uses.
STDMETHOD includes "virtual", the only difference with STDMETHODIMP so should I be using it? This is a COM class by the way. Even having moved the impl into the header I'm still getting "unnamed class template"
For now, use STDMETHOD in the class declaration and include the definition of the function there too. Fix the compile errors, verify the runtime, then separate out the implementation.
Might the issue be somewhere else? If I am extending MyClass, is this ok: class My2ndClass : public MyClass<AnotherClass>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.