Problem with member function of a templated class returning a pointer to an object of the same class

Discussion in 'C++' started by Jahagirdar Vijayvithal S, Aug 5, 2005.

  1. I am trying to compile the code below but get an error of

    /tmp/a.cpp: In function `int main()':
    /tmp/a.cpp:15: no matching function for call to `a<int>::foo(const
    char[4])'

    ----------------Start code-------------------------

    template <class T>
    class a {
    T var;
    public:
    template<class U> a<U>* foo(char *name);
    };



    int main()
    {

    a<int> aa;
    aa.foo("jvs");
    }
    template<class T> template<class U> a<U>* a<T>::foo(char *name) {
    ///Some processing on "name"
    return (U*)0;
    }
    ----------------End code-------------------------


    The Intent of the code is to return an object of type a<U> after
    processing the string name.
    Any pointers on what I am doing wrong and the meaning of the error
    message?


    reposted by jvs for girish

    Regards
    Jahagirdar Vijayvithal S
    --
    Jahagirdar .V.S
    IC Design Engineer , Texas Instruments (India) Ltd.
    91-80-25099129(O) 91-80-28540394(R)
     
    Jahagirdar Vijayvithal S, Aug 5, 2005
    #1
    1. Advertising

  2. Re: Problem with member function of a templated class returning apointer to an object of the same class

    Jahagirdar Vijayvithal S wrote:
    > I am trying to compile the code below but get an error of
    >
    > /tmp/a.cpp: In function `int main()':
    > /tmp/a.cpp:15: no matching function for call to `a<int>::foo(const
    > char[4])'
    >
    > ----------------Start code-------------------------
    >
    > template <class T>
    > class a {
    > T var;
    > public:
    > template<class U> a<U>* foo(char *name);


    If you intend to pass string literals here, the argument should be
    a pointer to const char.

    > };
    >
    >
    >
    > int main()
    > {
    >
    > a<int> aa;
    > aa.foo("jvs");


    What's 'U' for this call? The 'foo' is a member template, that means
    it needs _its_own_ template argument to be instantiated. How should
    the compiler figure it out? It can't. There is no information for it
    to deduce the 'U' type. So, you have to tell it yourself:

    aa.foo<int>("jvs");

    > }
    > template<class T> template<class U> a<U>* a<T>::foo(char *name) {
    > ///Some processing on "name"
    > return (U*)0;


    What is this bogus cast doing here? The return value type is not 'U*',
    it's 'a<U>*'. And in general, there is no need to cast 0 to a pointer,
    the Standard conversion exist for that, so just write

    return 0;

    > }
    > ----------------End code-------------------------
    >
    >
    > The Intent of the code is to return an object of type a<U> after
    > processing the string name.
    > Any pointers on what I am doing wrong and the meaning of the error
    > message?


    See above.

    V
     
    Victor Bazarov, Aug 5, 2005
    #2
    1. Advertising

  3. * Victor Bazarov <> wrote:
    <snip>
    >> template<class U> a<U>* foo(char *name);

    >

    <snip>
    >> a<int> aa;
    >> aa.foo("jvs");

    >
    > What's 'U' for this call? The 'foo' is a member template, that means
    > it needs _its_own_ template argument to be instantiated. How should
    > the compiler figure it out? It can't. There is no information for it
    > to deduce the 'U' type. So, you have to tell it yourself:
    >
    > aa.foo<int>("jvs");

    Thanks! I was struggling with this code for quite a few days and was not
    able to map the error message with the absence of <int>.

    Regards
    Jahagirdar Vijayvithal S
    --
    Features should be discovered, not documented.
    Jahagirdar .V.S
    IC Design Engineer , Texas Instruments (India) Ltd.
    91-80-25099129(O) 91-80-28540394(R)
     
    Jahagirdar Vijayvithal S, Aug 7, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fraser Ross
    Replies:
    4
    Views:
    1,066
    Fraser Ross
    Aug 14, 2004
  2. Replies:
    12
    Views:
    729
    fluden
    Feb 11, 2005
  3. Amadeus W. M.
    Replies:
    2
    Views:
    405
    Amadeus W. M.
    Jul 4, 2006
  4. Replies:
    1
    Views:
    310
  5. chhenning
    Replies:
    5
    Views:
    372
    chhenning
    Feb 13, 2008
Loading...

Share This Page