template function specialization problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bari

    #1

    template function specialization problem

    Hi,

    When i do the following specializition which commented in the source then
    including the header in two cpp files gives me an error:
    test3.obj : error LNK2005: "class A<int* __cdecl makeA<int>(void )"
    (??$makeA@H@@YA PAV?$A@H@@XZ) already defined in testSTL.obj

    Can you please tell me how i can specialize the template function and still
    use the header in multiple cpp files.

    Thank you in advance!

    Bar



    //HEADER
    #pragma once

    template <class Tclass A;
    template <class TA<T>* makeA();

    template <typename T>
    class A{
    private:
    A(){}
    T _a;
    public:
    friend A *makeA<>(); //error in g++-3.4, works with g++3.3
    };

    template <typename T>
    A<T>* makeA(){
    return new A<T>;
    }

    //SPECIALIZATION GIVING PROBLEM
    template <>
    A<int>* makeA(){
    return new A<int>;
    }
    //END PROBLEM SPECIALIZATION

    // end header

    //CPP

    int _tmain(int argc, _TCHAR* argv[])
    {
    A<int>* a = makeA<int>();
    }



  • Adrian Hawryluk

    #2
    Re: template function specialization problem

    Bari wrote:
    Hi,
    >
    When i do the following specializition which commented in the source then
    including the header in two cpp files gives me an error:
    test3.obj : error LNK2005: "class A<int* __cdecl makeA<int>(void )"
    (??$makeA@H@@YA PAV?$A@H@@XZ) already defined in testSTL.obj
    >
    Can you please tell me how i can specialize the template function and still
    use the header in multiple cpp files.
    >
    Thank you in advance!
    >
    Bar
    >
    >
    >
    //HEADER
    #pragma once
    >
    template <class Tclass A;
    template <class TA<T>* makeA();
    >
    template <typename T>
    class A{
    private:
    A(){}
    T _a;
    public:
    friend A *makeA<>(); //error in g++-3.4, works with g++3.3
    };
    >
    template <typename T>
    A<T>* makeA(){
    return new A<T>;
    }
    >
    //SPECIALIZATION GIVING PROBLEM
    template <>
    A<int>* makeA(){
    return new A<int>;
    }
    //END PROBLEM SPECIALIZATION
    >
    // end header
    >
    //CPP
    >
    int _tmain(int argc, _TCHAR* argv[])
    {
    A<int>* a = makeA<int>();
    }
    I tried your code, but I couldn't get it to compile with _tmain(). I'm
    not familiar with using TCHAR so, it is probably something that was not
    included. Replacing tmain with a regular main signature, it compiled
    under g++ 3.4.4 without problem, using it in a header that you gave.

    I would try doing a clean then remake your source. I would say it is
    probably some phase error. That or a bug in g++ 3.4 ;).


    Adrian

    --
    _______________ _______________ _______________ _______________ _________
    \/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
    \ My newsgroup writings are licensed under the Creative Commons /
    \ Attribution-Noncommercial-Share Alike 3.0 License /
    \_____[http://creativecommons.org/licenses/...sa/3.0/]_____/
    \/______[blog:__http://adrians-musings.blogspo t.com/]______\/

    Comment

    Working...