Does Visual C++ support member function template?

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

    Does Visual C++ support member function template?

    Hi,
    Does Visual C++ support member function template? I have the
    following code which can not get through VC6
    compiler.

    In header,
    class myclass{
    public:
    template<class T>
    static std::vector<T> getIntersection (std::vector<T> & ,
    std::vector<T>& );
    };

    In Cpp file,
    template<class T>
    vector<T> myclass::getInt ersection(vecto r<T>& v1, vector<T>&
    v2){
    vector<T> v;
    vector<T>::iter ator itr1;

    if (v1.size() == 0){
    return v;
    }

    ......
    return v;
    }

    This code passes SUN Forte compiler, but VC6 complains on this line,

    if (v1.size() == 0){

    as " error C2228: left of '.size' must have class/struct/union
    type".

    Do you know what's wrong here?

    Thanks a lot.


  • Moonlit

    #2
    Re: Does Visual C++ support member function template?

    Hi,


    "Jee" <jee@hotmail.co m> wrote in message
    news:3F871B4B.A 55BA19C@hotmail .com...[color=blue]
    > Hi,
    > Does Visual C++ support member function template? I have the[/color]
    Yes, VC6 and later in any case[color=blue]
    > following code which can not get through VC6
    > compiler.
    >
    > In header,
    > class myclass{
    > public:
    > template<class T>
    > static std::vector<T> getIntersection (std::vector<T> & ,
    > std::vector<T>& );
    > };
    >
    > In Cpp file,
    > template<class T>
    > vector<T> myclass::getInt ersection(vecto r<T>& v1, vector<T>&
    > v2){
    > vector<T> v;
    > vector<T>::iter ator itr1;
    >
    > if (v1.size() == 0){
    > return v;
    > }
    >
    > ......
    > return v;
    > }
    >[/color]
    Try moving the template function body to the header might help. I am not
    sure how the compiler is going to generate code if a part is in the cpp
    file. How does it know what it should generate if you fill in a certain
    type?


    Regards, Ron AF Greve.

    [color=blue]
    > This code passes SUN Forte compiler, but VC6 complains on this line,
    >
    > if (v1.size() == 0){
    >
    > as " error C2228: left of '.size' must have class/struct/union
    > type".
    >
    > Do you know what's wrong here?
    >
    > Thanks a lot.
    >
    >[/color]


    Comment

    • Moonlit

      #3
      Re: Does Visual C++ support member function template?

      Hi,

      Don't think my reply was very clear. What I meant was that if you include
      the header from another cpp file (not the one with the template body) how
      would it know what the body should look like.

      Regards


      Comment

      • David B. Held

        #4
        Re: Does Visual C++ support member function template?

        "Jee" <jee@hotmail.co m> wrote in message
        news:3F871B4B.A 55BA19C@hotmail .com...[color=blue]
        > [...]
        > Does Visual C++ support member function template? I
        > have the following code which can not get through VC6
        > compiler.
        > [...][/color]

        It has limited and flaky member template function support.
        Try asking in a Microsoft newsgroup.

        Dave



        ---
        Outgoing mail is certified Virus Free.
        Checked by AVG anti-virus system (http://www.grisoft.com).
        Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003


        Comment

        Working...