This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/TMS320F2812: Static member of template class

Part Number: TMS320F2812

Tool/software: TI C/C++ Compiler

Hello all!  CCS 7.1/TI v16.9.1.LTS/TMS320F2812/

I am try to compile my template:

//ModBus.h

template <class T> class Collection
{

public:
static int count; //static declaration
Collection()

{

count++;

}

};

template<class T>
int Collection<T>::count;

//ModBus.cpp

#include "ModBus.h"

Collection<float> CollectionObject;

void ModBusTables_Init(void)
{

Collection<float>::count = 6;

}

I take compile msg

I found my situation on this page  

http://processors.wiki.ti.com/index.php/C%2B%2B_Template_Instantiation_Issues

 on chapter called Third Workaround: Use a Template in a Non-Template Class Declaration.

The user who uses my template should declare himself every static member for each type he wants to use in template parametr? I do not like this

How can i solve this problem? Thank you!

  • Consider implementing the "Singleton" pattern. In this object, your "count" variable would be private, but not static. On demand, the class would allocate one and only one object of the singleton type, and its member "count" would be the one all of the accessors would refer to. It would be the one and only one count, and would thus be kind of like the static variable you have now.
  • It is good idea! Than you!

    I am trying to write singleton

    But the compiler gives me a warning "#1369-D static local variables of extern inline function are not resolved to single copy. Not ANSI C++ Compliant"

    Does the compiler convert a static class method to an inline function?

  • Write it like so:

    class userModBusSegment: [blah]
    {
    private:
        [blah]
        static userModBusSegment *theUserSegment;
        [blah]
    public:
        [blah]
        userModBusSegment *UserSegment()
        {
           if (!theUserSegment) theUserSegment= new userModBusSegment();
           return theUserSegment;
        }
        [blah]
        UserSegment()->Data_Size = [blah];
        [blah]
    };
    template 
    userModBusSegment<T_DataStruct, T_DataType> *
    userModBusSegment<T_DataStruct, T_DataType>::theUserSegment = NULL;
    

    There are other ways to implement Singleton, so don't take this as the only way to go.