-2

Thanks to this post, I'm trying to learning Curiously recurring template pattern.

This is the code I wrote:

#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

template<class EnvelopeType>
class Envelope
{
public:
    double mSingle = 1.0;

    void SetValue(double value) { mValue = value; }
    inline double GetValue() { return mValue; }

private:
    static double mValue;
};

class Volume : public Envelope<Volume>
{
};

class Pitch : public Envelope<Pitch>
{
};

template<EnvelopeType T>
double Envelope<T>::mValue;

int main()
{
    Envelope<Volume> mVolume;
    Envelope<Pitch> mPitch;

    mVolume.SetValue(0.5);
    mPitch.SetValue(1.0);
}

What's wrong? It says unknown type name "EnvelopeType". Of course it doesn't know: that's the T type of the template.

Community
  • 1
  • 1
markzzz
  • 47,390
  • 120
  • 299
  • 507
  • `template` what is this? – 463035818_is_not_an_ai Feb 15 '17 at 09:38
  • A CRTP doesn't make much sense with this regardless. Neither `Volume` nor `Pitch` are actually used *anywhere* besides as template parameter arguments. If you had `Volume mVolume;` etc. in your `main()` this would make more sense. The lack of needing those extra classes was why the [example I linked](http://coliru.stacked-crooked.com/a/9bac589e88912052) in your [prior question](https://stackoverflow.com/questions/42244021/which-kind-of-pattern-should-i-use-for-this-kind-of-task) (since deleted) used non-type arguments instead. – WhozCraig Feb 15 '17 at 09:53

2 Answers2

2

This

template<EnvelopeType T>
double Envelope<T>::mValue;

should actually look like

template<typename T>
double Envelope<T>::mValue;

since you are defining a variable inside an Evelope with some T type.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
2

When you write template <EnvelopeType T> then this is a template which takes one non-type argument T of type EnvelopeType. You're getting the "unknown type" error because there is no type called EnvelopeType in that context. In your code, the type EnvelopeType is only visible as a type in the definition of template<class EnvelopeType> class Envelope.

Instead, you probably meant to write this:

template <typename T>
double Envelope<T>::mValue;
jotik
  • 17,044
  • 13
  • 58
  • 123