473,748 Members | 7,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

linking static template variable

// - in my xy.cpp file --

template
<const int Ttex, const int Tcol, const int Tlight, int TzBuffer>
struct MyFragmentShade r
{
static const int varying_count = Ttex*2 + Tcol*3 + Tlight;
};

....
template<const int MyFragmentShade r<0,1,0,1>::var ying_count;
....
void foo()
{
// 'r' is of type super_complicat ed_class
r.fragment_shad er<MyFragmentSh ader<0,1,0,1();
}
// -- ends

The result is:

undefined reference to `MyFragmentShad er<0, 1, 0, 1>::varying_cou nt'

I (try to) use GCC 4.0.2.

Can you please assist me?
Jan 2 '08 #1
5 2009
On 2 ÑÎ×, 19:42, "Gernot Frisch" <M...@Privacy.n etwrote:
// - in my xy.cpp file --

template
<const int Ttex, const int Tcol, const int Tlight, int TzBuffer>
struct MyFragmentShade r
{
šstatic const int varying_count = Ttex*2 + Tcol*3 + Tlight;

};

...
template<const int MyFragmentShade r<0,1,0,1>::var ying_count;
...

void foo()
{
š š// 'r' is of type super_complicat ed_class
š šr.fragment_sha der<MyFragmentS hader<0,1,0,1() ;}

// -- ends

The result is:

undefined reference to `MyFragmentShad er<0, 1, 0, 1>::varying_cou nt'

I (try to) use GCC 4.0.2.

Can you please assist me?
(Warning! Highly unreliable opinion!) well, you haven't defined value
of that specialized constant (if this way of template specialization
is ever correct), you only have declared it, so linker doesn't find it.
Jan 2 '08 #2
(Warning! Highly unreliable opinion!) well, you haven't defined
value
of that specialized constant (if this way of template specialization
is ever correct), you only have declared it, so linker doesn't find
it.

// code----------
template
<const int Ttex, const int Tcol, const int Tlight, int TzBuffer>
struct MyFragmentShade r
{
static const int varying_count = Ttex*2 + Tcol*3 + Tlight;
};

#define VARCNT(t,c,l) template<const int
MyVertexShader< 1,1,1>::attribu te_count= t*2 + c*3 + l;
VARCNT(1,1,1);
VARCNT(1,1,0);
VARCNT(1,0,1);
VARCNT(1,0,0);
VARCNT(0,1,1);
VARCNT(0,1,0);
VARCNT(0,0,1);
VARCNT(0,0,0);
#undef VARCNT
// -------ends

yields:
error: duplicate initialization of MyVertexShader< 1, 1,
1>::attribute_c ount

....I'm so, so lost here.
Jan 2 '08 #3
Gernot Frisch wrote:
>(Warning! Highly unreliable opinion!) well, you haven't defined
value
of that specialized constant (if this way of template specialization
is ever correct), you only have declared it, so linker doesn't find
it.


// code----------
template
<const int Ttex, const int Tcol, const int Tlight, int TzBuffer>
struct MyFragmentShade r
{
static const int varying_count = Ttex*2 + Tcol*3 + Tlight;
};

#define VARCNT(t,c,l) template<const int
MyVertexShader< 1,1,1>::attribu te_count= t*2 + c*3 + l;
So, is it 'varying_count' or 'attribute_coun t'?
VARCNT(1,1,1);
VARCNT(1,1,0);
VARCNT(1,0,1);
VARCNT(1,0,0);
VARCNT(0,1,1);
VARCNT(0,1,0);
VARCNT(0,0,1);
VARCNT(0,0,0);
#undef VARCNT
// -------ends

yields:
error: duplicate initialization of MyVertexShader< 1, 1,
1>::attribute_c ount

...I'm so, so lost here.
Since you have the initialisation in the declaration (in the
class template definition), you should omit any initialisation
when defining your specialisations . That's what the compiler
is telling you here.

However, going back to your original inquiry, you need to use
your static somehow to cause the _instantiation_ of the static
member. It is likely that you didn't (at least the code you
posted didn't show any *use* of varying_count'.

Perhaps you want to repost *the actual code* that exhibits the
error...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 2 '08 #4

Found it, sorry for bothering!

#define VARCNT(t,c,l) \
template<const int \
MyVertexShader< \
/* here is the bug */ \
t,c,l /* instead of 1,1,1! */ \
>::attribute_co unt= t*2 + c*3 + l;
and one must not define the value in the declaration, then.

template
<const int Ttex, const int Tcol, const int Tlight, int TzBuffer>
struct MyFragmentShade r
{
static const int varying_count; // = Ttex*2 + Tcol*3 + Tlight;
};
Jan 2 '08 #5
On Jan 2, 4:54 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Gernot Frisch wrote:
(Warning! Highly unreliable opinion!) well, you haven't
defined value of that specialized constant (if this way of
template specialization is ever correct), you only have
declared it, so linker doesn't find it.
I think that's actually correct.
// code----------
template
<const int Ttex, const int Tcol, const int Tlight, int TzBuffer>
struct MyFragmentShade r
{
static const int varying_count = Ttex*2 + Tcol*3 + Tlight;
};
#define VARCNT(t,c,l) template<const int
MyVertexShader< 1,1,1>::attribu te_count= t*2 + c*3 + l;
So, is it 'varying_count' or 'attribute_coun t'?
Different class template (MyVertexShader , instead of
MyFragmentShade r), so why not a different name for the static.
I think he's just trying to confuse us by showing parts of two
different classes.

And is there an initializer or not in the explicit
specialization? That's very important, because as in the
original posting:
template<const int MyFragmentShade r<0,1,0,1>::var ying_count;
without the initializer, this is a *declaration* of a
specialization. And as you know, an explicit specialization is
*not* a template, per se, and obeys the usual rules of
non-template declarations. Which means that if he uses it,
there'd better be one (and only one) definition in the program.
Somewhere, in a source file (not in the header), a
template<const int
MyFragmentShade r<0,1,0,1>::var ying_count = something ;
VARCNT(1,1,1);
VARCNT(1,1,0);
VARCNT(1,0,1);
VARCNT(1,0,0);
VARCNT(0,1,1);
VARCNT(0,1,0);
VARCNT(0,0,1);
VARCNT(0,0,0);
#undef VARCNT
// -------ends
yields:
error: duplicate initialization of MyVertexShader< 1, 1,
1>::attribute_c ount
...I'm so, so lost here.
What's to be lost about. You explicitely specialize
MyVertexShader< 1,1,1>::attribu te_count 8 times, with different
initializers.

Again, an explicit specialization behaves pretty much like a
normal declaration. You wouldn't expect something like:

int const SomeClass::foo = 1*2 + 1*3 + 1 ;
int const SomeClass::foo = 1*2 + 1*3 + 0 ;
int const SomeClass::foo = 1*2 + 0*3 + 1 ;
int const SomeClass::foo = 1*2 + 0*3 + 0 ;
int const SomeClass::foo = 0*2 + 1*3 + 1 ;
int const SomeClass::foo = 0*2 + 1*3 + 0 ;
int const SomeClass::foo = 0*2 + 0*3 + 1 ;
int const SomeClass::foo = 0*2 + 0*3 + 0 ;

to work, and what you've written is basically the same thing.

I suspect a typo in the macro, and what you meant was:
#define VARCNT(t,c,l) template<const int \
MyVertexShader< t,c,l>::attribu te_count= t*2 + c*3 + l;
But beware: this still generates a *definition*, and not just a
declaration. Invoke your series of VARCNT in a header file,
include the header in more than one translation unit, and you'll
have multiple definitions (which the compiler isn't required to
diagnose).
Since you have the initialisation in the declaration (in the
class template definition), you should omit any initialisation
when defining your specialisations .
No. An explicit specialization replaces the template code. I
think he's confused you with his mixing two different classes
and two different problems in the same posting.
That's what the compiler is telling you here.
However, going back to your original inquiry, you need to use
your static somehow to cause the _instantiation_ of the static
member. It is likely that you didn't (at least the code you
posted didn't show any *use* of varying_count'.
If the static wasn't used, he wouldn't get an undefined
reference. The problem is that it is used, but he's explicitly
told the compiler not to instantiate the template variant,
becvause he's providing this one himself.
Perhaps you want to repost *the actual code* that exhibits the
error...
That would be nice. Preferrably in two separate postings, one
for each error.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 2 '08 #6

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

Similar topics

4
3134
by: Graham Dumpleton | last post by:
When you have a template class with a static member variable, ie., template<typename T> class handle { public: static void* id() { return &id_; } private: static int id_; };
7
2478
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M> registrar; }; The constructor of Registrar does the registering when it is initialized.
16
2982
by: Eric | last post by:
I have a static class member variable as follows: struct A { static void Set (int i) { v = i; } static int& Get () { return v; } static int v; }; int A::v; // define A::v in the cpp file
7
12455
by: ank | last post by:
Hi, I was curious about how to define static data member of template class. Should I put the definition in a separate source file or in the same header file as its template class? And when this data will be initialized if it is used across many translation unit, assume that it has constructor and needs dynamic initialization? I have blindly searched for the answer but I still not thoroughly
4
1992
by: santosh | last post by:
Hello, I have a doubt about static variable. class B { private: static int x; public: static set(int y) {
15
1748
by: Improving | last post by:
I have a template class that has static members, so in the .cpp file I have defined them with templated definitions. Now when in a different ..cpp file that includes the header file for the template class I derive a new class from the template class and pass that class as its template parameter (CRTP). Now when I instantiate this new class it complains it cannot find the statics. Doesnt the template definition define them? The code...
3
5710
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I have a base class with several sub classes. In runtime I want to create a instance of a sub class and assign it to a base class pointer. Nothing fancy about that. I also want to be able in runtime to decide witch type of sub class that is to be...
5
5057
by: ciccio | last post by:
Hi, I have a problem with my code that the compiler does not find any inline functions which are static. The simple code example is written below, this is what the compiler throws at me. ] $ g++ main.cpp foo.cpp /home/klaas/tmp/cciAcYgl.o: In function `main':
4
5823
by: aaragon | last post by:
Hi everyone, I have a linking error when using gcc4.2 and static member variables. The class template definition is something around the following: template<> class Element<L2_t: public Element_common<L2, Side<2,2 { public:
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9321
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.