Calling a Template member function without specifying a type

Discussion in 'C++' started by ogtindeed, Feb 10, 2004.

  1. ogtindeed

    ogtindeed Guest

    Hi all,

    I need some help. I am trying to instantiate a Template and call a
    member function, but have not been able -

    Now this is what I am trying to do:

    foo.h
    =========

    template<typename foo1>
    class myClass
    {
    public:
    void display(foo1);
    };


    template <typename foo1>
    void myClass<foo1>::display(foo1 outdata)
    {
    cout<<outdata<<"\n";
    }

    foo.cpp
    ========

    int main(int argc, char* argv[])
    {
    myClass mc; // compiler complains here-Is there anyway around
    this?
    myClass<char> mc; //compiler accepts this-But I don't want this?
    return (0);
    }


    Many thanks for the help. I appreciate -
    >t
    ogtindeed, Feb 10, 2004
    #1
    1. Advertising

  2. "ogtindeed" <> wrote in message
    news:...
    > Hi all,
    >
    > I need some help. I am trying to instantiate a Template and call a
    > member function, but have not been able -
    >
    > Now this is what I am trying to do:
    >
    > foo.h
    > =========
    >
    > template<typename foo1>
    > class myClass
    > {
    > public:
    > void display(foo1);
    > };
    >
    >
    > template <typename foo1>
    > void myClass<foo1>::display(foo1 outdata)
    > {
    > cout<<outdata<<"\n";
    > }
    >
    > foo.cpp
    > ========
    >
    > int main(int argc, char* argv[])
    > {
    > myClass mc; // compiler complains here-Is there anyway around
    > this?


    Nope. Class template arguments are never deduced. Even if they were,
    you haven't given the compiler any clues as to what the template
    arguments should be. What are you trying to achieve?

    Jonathan
    Jonathan Turkanis, Feb 10, 2004
    #2
    1. Advertising

  3. ogtindeed

    tom_usenet Guest

    On 10 Feb 2004 02:03:29 -0800, (ogtindeed) wrote:

    >Hi all,
    >
    >I need some help. I am trying to instantiate a Template and call a
    >member function, but have not been able -
    >
    >Now this is what I am trying to do:
    >
    >foo.h
    >=========
    >


    You can default template parameters:

    template<typename foo1 = char>

    >class myClass
    >{
    > public:
    > void display(foo1);
    >};


    >int main(int argc, char* argv[])
    >{
    > myClass mc; // compiler complains here-Is there anyway around
    >this?
    > myClass<char> mc; //compiler accepts this-But I don't want this?
    > return (0);
    >}


    I can only assume you want defaults. Or perhaps you just need a
    non-template? Or a member template? More detail is required...

    Tom

    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    tom_usenet, Feb 10, 2004
    #3
  4. "tom_usenet" <> wrote in message
    news:...
    | On 10 Feb 2004 02:03:29 -0800, (ogtindeed) wrote:

    [snip]

    Hi Tom.

    | >I need some help. I am trying to instantiate a Template and call a
    | >member function, but have not been able -
    | >
    | >Now this is what I am trying to do:
    | >
    | >foo.h
    | >=========
    | >
    |
    | You can default template parameters:
    |
    | template<typename foo1 = char>

    But that would require that you declare the following as such:

    myClass<> mc;

    | > myClass mc; // compiler complains here-Is
    // there anyway around this?

    [snip]

    Which doesn't really do what the OP wanted.

    I think you're right though, the OP really
    needs to be more specific in what s/he requires.

    Cheers.
    Chris Val
    Chris \( Val \), Feb 10, 2004
    #4
    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. Replies:
    1
    Views:
    473
    Victor Bazarov
    Jul 20, 2005
  2. /M
    Replies:
    1
    Views:
    352
    Gianni Mariani
    Nov 14, 2006
  3. Peng Yu
    Replies:
    3
    Views:
    772
    Thomas J. Gritzan
    Oct 26, 2008
  4. Hicham Mouline
    Replies:
    1
    Views:
    352
    Vladyslav Lazarenko
    Mar 27, 2009
  5. Richard A. DeVenezia
    Replies:
    4
    Views:
    122
    Lasse Reichstein Nielsen
    Sep 5, 2003
Loading...

Share This Page