473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Linker problem with template specialisation

I have the following code:

enum foo
{
null_foo,
foo1,
foo2
};

template<typename T>
struct data
{
T val;
bool set;

template<typename U>
data<T>& operator=(const U &that)
{
val = that;
set = true;
return *this;
}
};

struct foo_1 { };
struct foo_2 { };

template<typename Tstruct map_foo_type { static foo const id =
null_foo; }; // worst match
template<struct map_foo_type<foo_1{ static foo const id = foo1; };
template<struct map_foo_type<foo_2{ static foo const id = foo2; };

Using the above code, I want to be able to do the following:

data<intmid = map_foo_type<foo1>::id;

When I compile with gcc 3.3.6 I get a linker error - undefined symbol
mep_foo_type<foo1>::id

If I have an intermediate step:

foo id = map_foo_type<foo1>::id
data<intmid = id;

then it links fine.

Alternately, if I change operator= above to take const U data, rather
than a reference to const U, it also links fine!

What is the problem please?

Thanks
Steve
Jun 27 '08 #1
4 1222


tso...@gmail.com napsal:
I have the following code:

enum foo
{
null_foo,
foo1,
foo2
};

template<typename T>
struct data
{
T val;
bool set;

template<typename U>
data<T>& operator=(const U &that)
{
val = that;
set = true;
return *this;
}
};

struct foo_1 { };
struct foo_2 { };

template<typename Tstruct map_foo_type { static foo const id =
null_foo; }; // worst match
template<struct map_foo_type<foo_1{ static foo const id = foo1; };
template<struct map_foo_type<foo_2{ static foo const id = foo2; };

Using the above code, I want to be able to do the following:

data<intmid = map_foo_type<foo1>::id;

When I compile with gcc 3.3.6 I get a linker error - undefined symbol
mep_foo_type<foo1>::id

If I have an intermediate step:

foo id = map_foo_type<foo1>::id
data<intmid = id;

then it links fine.

Alternately, if I change operator= above to take const U data, rather
than a reference to const U, it also links fine!

What is the problem please?

Thanks
Steve
data<intmid = map_foo_type<foo1>::id; calls constructor taking one
parameter. It is not assignment operator. Just define constructor in
simillar way as assignment operator and it should work.
Jun 27 '08 #2


tso...@gmail.com napsal:
I have the following code:

enum foo
{
null_foo,
foo1,
foo2
};

template<typename T>
struct data
{
T val;
bool set;

template<typename U>
data<T>& operator=(const U &that)
{
val = that;
set = true;
return *this;
}
};

struct foo_1 { };
struct foo_2 { };

template<typename Tstruct map_foo_type { static foo const id =
null_foo; }; // worst match
template<struct map_foo_type<foo_1{ static foo const id = foo1; };
template<struct map_foo_type<foo_2{ static foo const id = foo2; };

Using the above code, I want to be able to do the following:

data<intmid = map_foo_type<foo1>::id;

When I compile with gcc 3.3.6 I get a linker error - undefined symbol
mep_foo_type<foo1>::id

If I have an intermediate step:

foo id = map_foo_type<foo1>::id
data<intmid = id;

then it links fine.

Alternately, if I change operator= above to take const U data, rather
than a reference to const U, it also links fine!

What is the problem please?

Thanks
Steve
data<intmid = map_foo_type<foo1>::id; calls constructor taking one
parameter. It is not assignment operator. Just define constructor in
simillar way as assignment operator and it should work.
Jun 27 '08 #3
On 29 Kvě, 11:44, tso...@gmail.com wrote:
I have the following code:

enum foo
{
null_foo,
foo1,
foo2

};

template<typename T>
struct data
{
T val;
bool set;

template<typename U>
data<T>& operator=(const U &that)
{
val = that;
set = true;
return *this;
}

};

struct foo_1 { };
struct foo_2 { };

template<typename Tstruct map_foo_type { static foo const id =
null_foo; }; // worst match
template<struct map_foo_type<foo_1{ static foo const id = foo1; };
template<struct map_foo_type<foo_2{ static foo const id = foo2; };

Using the above code, I want to be able to do the following:

data<intmid = map_foo_type<foo1>::id;

When I compile with gcc 3.3.6 I get a linker error - undefined symbol
mep_foo_type<foo1>::id

If I have an intermediate step:

foo id = map_foo_type<foo1>::id
data<intmid = id;

then it links fine.

Alternately, if I change operator= above to take const U data, rather
than a reference to const U, it also links fine!

What is the problem please?

Thanks
Steve
Sorry for double post of previous comment.

You should also change map_foo_type<foo1>::id; to
map_foo_type<foo>::id;, because template map_foo_type takes as
argument typename, not type foo. So you have to pass there foo,
instead of foo1.
Jun 27 '08 #4
On May 29, 5:44*pm, tso...@gmail.com wrote:
I have the following code:

enum foo
{
* * null_foo,
* * foo1,
* * foo2

};

template<typename T>
struct data
{
* * T val;
* * bool set;

* * template<typename U>
* * data<T>& operator=(const U &that)
* * {
* * * * val = that;
* * * * set = true;
* * * * return *this;
* * }

};

struct foo_1 { };
struct foo_2 { };

template<typename Tstruct map_foo_type { static foo const id =
null_foo; }; // worst match
template<struct map_foo_type<foo_1{ static foo const id = foo1; };
template<struct map_foo_type<foo_2{ static foo const id = foo2; };
initialize them outside of the class.
template<typename Tstruct map_foo_type
{ static foo const id; }; // worst match

template <typename T>
foo const map_foo_type<T>::id = null_foo;

template<struct map_foo_type<foo_1{ static foo const id; };
template<struct map_foo_type<foo_2{ static foo const id; };

foo const map_foo_type<foo_1>::id = foo1;
foo const map_foo_type<foo_2>::id = foo2;
>
Using the above code, I want to be able to do the following:

* * data<intmid = map_foo_type<foo1>::id;
foo_1

here calls copy constructor other than assignment operator as marked
else thread
>
When I compile with gcc 3.3.6 I get a linker error - undefined symbol
mep_foo_type<foo1>::id

If I have an intermediate step:

foo id = map_foo_type<foo1>::id
data<intmid = id;

then it links fine.

Alternately, if I change operator= above to take const U data, rather
than a reference to const U, it also links fine!

What is the problem please?

Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Tim Clacy | last post by:
Your expertise will be appreciated... Here's a general templatised class; all specialisations of this class should have a pointer to a specialisation of the same, templatised type: ...
8
by: Douglas | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** Hello, The following code does not compile if line 3 is uncommented "using namespace std". I do not understand it. Could...
2
by: Martin MacRobert | last post by:
Hi, I'm trying to make a specialisation of a template function, so that the second parameter accepts scalar types only (int,double,float etc.). How can I do this without writing an explicit...
2
by: Marc Mutz | last post by:
Hi, I'm currently wondering if and how you can separate definition from declaration of a full template specialisation. I have a UnitTest class that has an _assertEqual member template that I...
2
by: Stephen Starkie | last post by:
Hi, For a while I have had some problem understanding just how template specialisation works in certain cases. In abridged form my code looks like this; --MyTemplate.h-- #ifndef MyTemplateH...
2
by: Pete C | last post by:
Hello... I am writing a templated 'wrapper' class that takes as its template parameter a class to be inherited from. I need a specialisation of this wrapper class in certain cases, however I am...
8
by: Paul Roberts | last post by:
Hi, I'm hoping somebody here can help me with a simple problem of template syntax. Here's an example: template<typename T, int iclass A { static int a;
8
by: Rahul | last post by:
Hi, Is there a way to partially specialize only a member function of a template class (not the whole class). e.g. template <typename A, typename B> class Base { public:
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.