A template question.

Discussion in 'Java' started by Spark, Feb 21, 2007.

  1. Spark

    Spark Guest

    I have a question about type parameters. Class Log is concrete one I
    expected.

    Log l;
    l.setValue(10);
    l.setValue("value");
    l.setValue(10.0);

    I tried to define this class as following. but got compile time error.

    template<typename T>
    class Log {
    public:
    void setValue(T s){
    cout << s << endl;
    }
    };

    Does anyone point me out?

    Thanks in advance.
     
    Spark, Feb 21, 2007
    #1
    1. Advertising

  2. Spark

    Ian Shef Guest

    "Spark" <> wrote in news:1172083224.505757.295270
    @p10g2000cwp.googlegroups.com:

    > I have a question about type parameters. Class Log is concrete one I
    > expected.

    <snip>
    > template<typename T>
    > class Log {
    > public:
    > void setValue(T s){
    > cout << s << endl;
    > }
    > };
    >
    > Does anyone point me out?

    <snip>

    You will get a much better response in a C++ newsgroup than in this one which
    is for Java.

    --
    Ian Shef 805/F6 * These are my personal opinions
    Raytheon Company * and not those of my employer.
    PO Box 11337 *
    Tucson, AZ 85734-1337 *
     
    Ian Shef, Feb 21, 2007
    #2
    1. Advertising

  3. Spark

    Daniel Pitts Guest

    On Feb 21, 10:40 am, "Spark" <> wrote:
    > I have a question about type parameters. Class Log is concrete one I
    > expected.
    >
    > Log l;
    > l.setValue(10);
    > l.setValue("value");
    > l.setValue(10.0);
    >
    > I tried to define this class as following. but got compile time error.
    >
    > template<typename T>
    > class Log {
    > public:
    > void setValue(T s){
    > cout << s << endl;
    > }
    >
    > };
    >
    > Does anyone point me out?
    >
    > Thanks in advance.


    Even though you should have posted in the c++ newsgroup, I'll help
    you..

    You have defined a class Log, which is parameterized, what you wanted
    to do was to create a function setValue which is parameterize.

    class Log {
    public:
    template<typename T>
    void setValue(T s) {
    cout << s << endl;
    }
    }
     
    Daniel Pitts, Feb 21, 2007
    #3
  4. Spark

    Spark Guest

    On 2ÔÂ22ÈÕ, ÉÏÎç6ʱ21·Ö, "Daniel Pitts" <> wrote:
    > On Feb 21, 10:40 am, "Spark" <> wrote:
    >
    >
    >
    > > I have a question about type parameters. Class Log is concrete one I
    > > expected.

    >
    > > Log l;
    > > l.setValue(10);
    > > l.setValue("value");
    > > l.setValue(10.0);

    >
    > > I tried to define this class as following. but got compile time error.

    >
    > > template<typename T>
    > > class Log {
    > > public:
    > > void setValue(T s){
    > > cout << s << endl;
    > > }

    >
    > > };

    >
    > > Does anyone point me out?

    >
    > > Thanks in advance.

    >
    > Even though you should have posted in the c++ newsgroup, I'll help
    > you..
    >
    > You have defined a class Log, which is parameterized, what you wanted
    > to do was to create a function setValue which is parameterize.
    >
    > class Log {
    > public:
    > template<typename T>
    > void setValue(T s) {
    > cout << s << endl;
    > }
    >
    > }


    I'm sorry,

    I use google group. I really want to post to c++ newgroups. but
    something wrong with my configuration, lead to wrong place.

    anyway, thank you for Daniel's reply. It's helpful for me.

    Spark
     
    Spark, Feb 22, 2007
    #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. Chris Theis
    Replies:
    2
    Views:
    494
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    560
    tom_usenet
    Jul 24, 2003
  3. Replies:
    1
    Views:
    2,141
    Gianni Mariani
    Jun 8, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    801
    Thomas J. Gritzan
    Oct 26, 2008
  5. nguillot
    Replies:
    5
    Views:
    550
Loading...

Share This Page