how initializate a template class static variable?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template<typename T>
class Form
{
public:
    static  int i;
    Form()
    {
        i++;
        cout << i << "\t";

    }
    ~Form()
    {

    }

};
int Form::i=0;

i get an error on:
int Form::i=0;
error message: "'template<class T> class Form' used without template parameters"
so how can i initilizate a template class static variable?
Why your class Form has a template parameter T, but it is never used anywhere in that class?

Anyhow, you use template classes like:
1
2
3
4
5
6
7
8
9
10
11
template<typename T> class MyClass
{
public:
    MyClass(void) : data(0) {}
    MyClass(const T value) : data(value) {}
private:
    T data; // <-- let's actually use typename T here!
};

static MyClass<int> instance; // <-- default constructor
static MyClass<int> instance2 = MyClass<int>(42); // <-- call explicit constructor with one argument 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

template<typename T>
  struct Form
  {
    static int i;
    Form() { std::cout << ++i << '\n'; }
  };

template <typename T> 
  int Form<T>::i = 0;
  
  int main()
  {
    Form<int> i1;    // 1
    Form<int> i2;    // 2
    Form<int> i3;    // 3
    Form<double> d1; // 1
  }
"Why your class Form has a template parameter T, but it is never used anywhere in that class?"
see these code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
template <class ParentClass>
class Form
{
public:
    static  int i;
    ParentClass *ChildPointer;

    Form(ParentClass *Parent)
    {
        i++;
        cout << i << "\t";
        ChildPointer=Parent;
    }


    ~Form()
    {

    }

};
template <typename T>
  int Form<T>::i = 0;

class Window: public Form<Window> { public: MouseEvents(); Window(): Form(this)  {  }  ~Window() { } }Window;
class Window2: public Form<Window2> { public: MouseEvents(); Window2(): Form(this)  {  }  ~Window2() { } }Window2;

why, on:
1
2
3
4
5
6
 Form(ParentClass *Parent)
    {
        i++;
        cout << i << "\t";
        ChildPointer=Parent;
    }

always, i get zero?ok... i have two classes: Window and Window2.
so why, always, the 'i' is zero? only add\increment once?
Last edited on
This program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using std::cout;

template <class ParentClass>
class Form
{
public:
    static  int i;
    ParentClass *ChildPointer;

    Form(ParentClass *Parent)
    {
        i++;
        cout << i << "\t";
        ChildPointer=Parent;
    }


    ~Form()
    {

    }

};
template <typename T>
  int Form<T>::i = 0;

class Window: public Form<Window> { public: /* MouseEvents(); */ Window(): Form(this)  {  }  ~Window() { } }Window;
class Window2: public Form<Window2> { public: /* MouseEvents(); */ Window2(): Form(this)  {  }  ~Window2() { } }Window2;

int main() {}


Outputs
1	1	


What do you expect the output of this program to be?
Last edited on
If theres 2 classes variables, it must be 2 and not 1
If theres 2 classes variables, it must be 2 and not 1

Please, be precise next time.

Did you expect
2	2	
Or
1	2	
Or
2	1	
Or
2	
Or something else? With this information, I could have given a better explanation.

Both Form<Window> and Form<Window2> are different classes.
Both Form<Window> and Form<Window2> have different static data members.

So the variables Form<Window>::i and Form<Window2>::i are different.

The output must be 2. But the Window2 is different, but the Form it is parent class. So how can i use a static variable on Form? The 'i' must be incremented when a child class is created
Sorry something
So how can i use a static variable on Form?
You may have a non templated base class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class FormBase
{
public:
    static  int i;
    FormBase()
    {
        i++;
        cout << i << "\t";
    }
};

template <class ParentClass>
class Form : public FormBase
{
public:
    static  int i;
    ParentClass *ChildPointer;

    Form(ParentClass *Parent)
    {
        i++;
        cout << i << "\t";
        ChildPointer=Parent;
    }


    ~Form()
    {

    }

};
Last edited on
i can't use it :(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class FormBase
{
public:
    static  int i;
    FormBase()
    {
        i++;
        cout << i << "\t";
    }
};

template <class ParentClass>
class Form : public FormBase
{
public:
    ParentClass *ChildPointer;

    Form(ParentClass *Parent)
    {
        ChildPointer=Parent;
    }


    ~Form()
    {

    }

};

int main()
{
    cout << Form<FormBase>::i; //error: undefined reference to `FormBase::i'
    return 0;
}

the 'Form' is for be used like a variable... or a class without objects
The output must be 2. But the Window2 is different, but the Form it is parent class. So how can i use a static variable on Form? The 'i' must be incremented when a child class is created


Form is not a class.
Form is a class template.

Form<A> and Form<B> are completely distinct class types.

So how can i use a static variable on Form? The 'i' must be incremented when a child class is created

Use a namespace-scope variable instead

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using std::cout;

extern int i;
template <typename T>
  class Form
  {
  public:
      Form()
      {
          i++;
          cout << i << "\t";
      }
  };

// in exactly one source file
int i;

class Window: public Form<Window> {}Window;
class Window2: public Form<Window2> {}Window2;
int main() {}


Last edited on
Afterwards, please burn all the code that uses a global variable called 'i' :)
i can't use it :(
Of course you need to initialize a static variable outside the class. See:

https://www.learncpp.com/cpp-tutorial/static-member-variables/

Note this: "a static inline int can be declared and initialized directly (C++17)"

1
2
3
4
5
6
7
8
9
10
11
12
class FormBase
{
public:
    static  int i;
    FormBase()
    {
        i++;
        cout << i << "\t";
    }
};

int FormBase::i = 0;


Or do what @mbozzi suggest.
Last edited on
i love these way:
static inline int FormCount=0;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Form
{
public:
    static inline int FormCount=0;
  Form()
  {
      FormCount++;
      cout << FormCount << "\t";
  }
};
class Window: public Form
{

}Window;

class Window2: public Form
{

}Window2;

int main()
{

    return 0;
}

output:
1 2

compiler options must have, at least, -std=C++17 or -std=gnu++17.
the static variable must be 'inline'.
thanks for all to all
can i do these:
typedef static inline Static;
?
error line:
" 'Static' does not name a type; did you mean 'static'?"

update: ate least, i can use macros:
#define Static static inline
PS: never use ';' after a macro hehehehe(not increment, just the 1st time hehehehe)
thank you so much
Last edited on
Registered users can post here. Sign in or register to post.