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.

C2000 v6.4.9: Issue with templates

Guru 20035 points

Hello, 

I am using C2000 compilier version v6.4.9.

I am trying to create a generic circular buffer using c++ templates

3755.TestCircularbufferTemplate.zip

The attached project generates the following compiler error:

void CircularBuffer::vPut<T1>(const T1 &) [with T1=unsigned char [32]] ./main.obj      

How can I fix the error?

Does the standard library already have a generic circular buffer class?

Thanks,

Stephen

  • Ok, I found I found some information at However, my implementation doesn't seem to work.

    Please see attached project.   TestCircularbufferTemplate3.zip

    It works without a template (i.e. foo(records)), why isn't it working with the template (i.e. recordsCircularBuffer.vPut(record).

    Stephen

  • Thanks for the info,

    I still get the error, i.e. 

     undefined                                                                               first referenced
      symbol                                                                                     in file     
     ---------                                                                               ----------------
     void CircularBuffer::vPut<T1, N2>(const T1 (&)[N2]) [with T1=unsigned char, N2=(int)32] ./main.obj  


    after I moved the template function to the header file, i.e.:

    #ifndef CIRCULAR_BUFFER_H
    #define CIRCULAR_BUFFER_H
    
    class CircularBuffer
    {
    public:
        CircularBuffer(void);
        template <typename T, int size>
        void vPut(const T (&array)[size]); // Adds data to circular buffer
    };
    
    #if 1
    template <typename T, int size>
    void foo(T (&array)[size])
    {
     //sort array, find the sum, min, max, etc
    }
    #endif
    
    #endif /* CIRCULAR_BUFFER_H */

    However, the program compiles without errors if remove the template function from the cpp file and define  it in the class, i.e.

    #ifndef CIRCULAR_BUFFER_H
    #define CIRCULAR_BUFFER_H
    
    class CircularBuffer
    {
    public:
        CircularBuffer(void);
        template <typename T, int size>
        void vPut(const T (&array)[size]){}; // Adds data to circular buffer
    };
    
    #endif /* CIRCULAR_BUFFER_H */

    Anyone at TI have any answers?

    Stephen

  • You must place the definition (body) of the template function vPut in the header file, not foo.

  • Thanks for the reply.

    I edited my previous post to change "When I move the template function to the header file, i.e.:" to "after I moved the template function to the header file, i.e.:"

    Ok, your suggestion fixed it.  I had accidentally moved the foo function to the header file.

    The attached zip file contains the project that compiles without errors.

    TestCircularbufferTemplate4.zip

    Thanks,

    Stephen