Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > A template question.

Reply
Thread Tools

A template question.

 
 
Spark
Guest
Posts: n/a
 
      02-21-2007
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.

 
Reply With Quote
 
 
 
 
Ian Shef
Guest
Posts: n/a
 
      02-21-2007
"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 *
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      02-21-2007
On Feb 21, 10:40 am, "Spark" <liang.sp...@gmail.com> 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;
}
}


 
Reply With Quote
 
Spark
Guest
Posts: n/a
 
      02-22-2007
On 2月22日, 上午6时21分, "Daniel Pitts" <googlegrou....@coloraura.com> wrote:
> On Feb 21, 10:40 am, "Spark" <liang.sp...@gmail.com> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments