A template question.

S

Spark

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.
 
I

Ian Shef

I have a question about type parameters. Class Log is concrete one I
expected.
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.
 
D

Daniel Pitts

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;
}
}
 
S

Spark

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top