Hi,
I run into error with the following program. Would you please help me?
Best wishes,
Peng
struct tag1{};
struct tag2{};
template <typename T>
inline void test(tag1){
}
template <typename T>
inline void test(tag2){
}
int main(){
test(tag1);//error with g++-3.4.4
} 6 1802 Pe*******@gmail.com wrote: Hi,
I run into error with the following program. Would you please help me?
Best wishes, Peng
struct tag1{}; struct tag2{};
template <typename T> inline void test(tag1){ }
template <typename T> inline void test(tag2){ }
int main(){ test(tag1);//error with g++-3.4.4
tag1 is not a variable, it is a type. I would guess that you're getting
a redeclaration error. }
<Pe*******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com... Hi,
I run into error with the following program. Would you please help me?
Best wishes, Peng
struct tag1{}; struct tag2{};
template <typename T> inline void test(tag1){ }
template <typename T> inline void test(tag2){ }
int main(){ test(tag1);//error with g++-3.4.4
The argument must be an instance of an object,
not the name of a type. In addition, since
'test' is a function template, and there's
no way to deduce one, a template argument must
be specified.
Try:
test<int>(tag1());
BTW I think you're obscuring things with your templates.
They're not needed at all.
struct tag1{};
struct tag2{};
void test(tag1){
}
void test(tag2){
}
int main(){
test(tag1());
return 0;
}
I think you need to read more about templates and what
they're for.
-Mike
<Pe*******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
| Hi,
|
| I run into error with the following program. Would you please help me?
|
| Best wishes,
| Peng
|
| struct tag1{};
| struct tag2{};
should be:
struct Tag1{};
struct Tag2{};
By convention and to save a programmer from having to pop aspirin for a
living, a capitalized name is reserved for structs and classes. So if
you see Tag1, its a type, while tag1 might be an instance of a Tag1
type.
|
| template <typename T>
| inline void test(tag1){
| }
tag1 here is a type, not an instance... you don't need multiple
definitions of the same template either. The following template can
potentially accept any class.
template< typename T >
void test( T& t ) // pass_by_reference
{
}
or
template< typename T >
void test( const T& t ) // pass_by_const_reference
{
// t is unmodifiable and therefore safe
}
|
| template <typename T>
| inline void test(tag2){
| }
above not needed...
|
| int main(){
| test(tag1);//error with g++-3.4.4
// error because tag1 was a type and test() doesn't exist.
Tag1 tag1; // tag1 is an instance of a Tag1 type.
Tag2 tag2;
test< Tag1 >(tag1); // templated<> with a Tag1 type,
// and tag1 is the object being passed
test< Tag2 >(tag2);
int n;
test< int >(n); // doing it with a primitive integer, and so on...
| }
|
If you really did mean to specialize the test template...then...
template< typename T = Tag1 >
void test( T& t )
{
// do this if T is Tag1
}
template<>
void test<Tag2>( T& t )
{
// do this if T is Tag2
}
int main()
{
Tag1 tag1;
test(tag1); // default T = Tag1
Tag2 tag2;
test<Tag2>(tag2); // you must tell the compiler
}
Peter_Julian wrote: <Pe*******@gmail.com> wrote in message news:11**********************@g47g2000cwa.googlegr oups.com... | Hi, | | I run into error with the following program. Would you please help me? | | Best wishes, | Peng |
.... If you really did mean to specialize the test template...then...
template< typename T = Tag1 > void test( T& t ) { // do this if T is Tag1 }
Only a class template can specify a default type parameter. A function
template may not.
template<> void test<Tag2>( T& t ) { // do this if T is Tag2 }
Since the only purpose of the function's parameter is to select the
specialization, I think test() should be specialized for Tag2 like so:
template <>
void test( Tag2 t)
{
}
Tag2 is an empty class - so there is no reason either to pass the
parameter by reference or to declare it const.
int main() { Tag1 tag1; test(tag1); // default T = Tag1
No, there is no default type possible here.
Tag2 tag2; test<Tag2>(tag2); // you must tell the compiler }
If the test function template deduced the specialized type from its
parameter as I suggested above, then calling test( tag2) would be
possible.
Greg
> The argument must be an instance of an object, not the name of a type.
Yes, true - arg must be an instance of type.
In addition, since 'test' is a function template, and there's no way to deduce one, a template argument must be specified.
Hmmm, ever heard of make functions - function templates determine
(deduce) the template parameters from the arguments passed (C++
template p.12). For his example this may be true. How do you think
std::min, std::max, std::make_pair can be called without explicitly
specifying the template parameters :0. Try:
test<int>(tag1());
.... or
test( tag1() ); //Should work to, and
//instantiate test<tag1>, which is what he wanted. BTW I think you're obscuring things with your templates. They're not needed at all.
Maybe true, but trying things out to come to grips is not so bad.
I think you need to read more about templates and what they're for.
IMHO you may require some touching up too. -Mike
Werner
Mike Wahler wrote: <Pe*******@gmail.com> wrote in message news:11**********************@g47g2000cwa.googlegr oups.com... Hi,
I run into error with the following program. Would you please help me?
Best wishes, Peng
struct tag1{}; struct tag2{};
template <typename T> inline void test(tag1){ }
template <typename T> inline void test(tag2){ }
int main(){ test(tag1);//error with g++-3.4.4 The argument must be an instance of an object, not the name of a type. In addition, since 'test' is a function template, and there's no way to deduce one, a template argument must be specified.
Please ignore my previous msg, I read the original posting too quick. I
took it for:
template <typename T>
inline void test( T )
{
}
struct Tag1{};
struct Tag2{};
int main()
{
test( Tag1() );
test( Tag2() );
//..or test( 10 );
}
because this would be the more obvious thing. In this case one would
not require test<int>( Tag1() ); //And of course it would not compile Try:
test<int>(tag1());
Yes, absolutely. BTW I think you're obscuring things with your templates. They're not needed at all.
Certainly, but he may be learning (fooling around to learn).
I think you need to read more about templates and what they're for.
He certainly does, and you don't (as I mentioned previously). -Mike
Regards (and sorry),
Werner This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: free2cric |
last post by:
Hi,
What will the syntax sound like for checking size of an empty class?
what will be the size.
Thanks,
Cric
|
by: gg |
last post by:
I am facing some problems with following program. I am using aCC
version 03.27 on HP-UX11. The command line I use to compile is -
aCC -AA -I. TestTempMethods.C
Can anybody pls suggest how to...
|
by: wayne hamilton |
last post by:
I'm having a problem Interacting with Command Line programs. I can read and
write anything I want as long as I don't have to interact with a process
once it's started. Originally I had been calling...
|
by: rolandz |
last post by:
Hi,
Maybe somebody has been fighting with the problem that I do, currently.
I have a class that has method f(). The two versions of the f() method
accept different objects: Int and Short. These...
|
by: rajanipro |
last post by:
Hi buddies!
Can you tell me how to invoke base class static method hidden by inheritance, using derived class as in the following case?
using System;
class ParentClass
{
public static...
|
by: python101 |
last post by:
class Test:
def __init__(self):
self._power = False
self._v = 5
self._station = 80.0
self._presets=
def togglePower(self): # when I tried it this did...
|
by: sailormoon |
last post by:
this information i put in data.txt
11221 MOHD IRFAN 80 70
11222 NURUL FITRAH 80 90
11223 MOHD FARHAN 70 80
11224 WAFFIN WARDAH 80 60
11225 SYAMSUL 50 50
99999 TAMAT 0 0
#include<fstream.h>
|
by: abhi97 |
last post by:
If derived class will be having vtable then why we need vtable for derived class?
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |