![]() |
| | #1 |
| Registered User Join Date: Oct 2005
Posts: 271
| Linking error with static template class members I have a templated class. This class has a static class member variable, which is a templated stl_container. Then there is a class derived from this one that tries to call this member variable in a source file, but results in a linker error. Here's some simplified code: Code: // in a header file
template <typename T>
class A {
static stl_container<T*>* container;
};
class B : public A<MyClass> {
void do_something();
};
//in a source file
//memory allocation for "container" is in different source file
void B::do_something() {
do_something_to(container);
}
So I can think of one solution which would be to dump the definition of "do_something" into the class body so it doesn't have to resolve the linking error. But I've got other non-class functions that have to access "container" so that's a no go. So how do you declare, allocate and use a templated static class variable? Is it possible? I need this thing to be static because "class A" is a actually a node in a graph structure that links to other nodes in the graph and info on the overall graph (kept in the container) can be updated by any node. |
| cunnus88 is offline | |
| | #2 |
| Registered User Join Date: Oct 2005
Posts: 271
| After some googling and tinkering, I've found a solution, but I can't say I understand why it works. Code: // in a header file
template <typename T>
class A {
static stl_container<T*>* container;
};
//you need a second declaration outside the body of the class
//in the header
template <typename T>
typename A<T>::stl_container<T*>* container;
|
| cunnus88 is offline | |
| | #3 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,759
| Why is that so surprising? You have to do that even without templates. Code: class A
{
public:
static int x;
};
int A::x;
int main()
{
A::x = 0;
return 0;
}
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot |
| brewbuck is offline | |
| | #4 |
| Registered User Join Date: Oct 2005
Posts: 271
| My previous understanding was that static member variables have to be defined outside the class body, not just declared. Isn't a statement like Code: int A::x; But when you have a template declaration Code: template <typename T> typename A<T>::stl_container<T*>* container; |
| cunnus88 is offline | |
| | #5 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,759
| Why should the template syntax deviate from the non-template syntax?
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot |
| brewbuck is offline | |
| | #6 |
| Registered User Join Date: Oct 2005
Posts: 271
| True. Consistency is important. |
| cunnus88 is offline | |
| | #7 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,781
| It's the same as with non-templates. It may be a declaration or implementation, but one instantiated, when the class is instantiated, it becomes a definition. That is generally a rule of templates - they are a declaration sorts of, but once instantiated, a definition for those particular types is created.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| LNK2001 ERROR!!! need help | lifeafterdeath | C++ Programming | 7 | 05-27-2008 05:05 PM |
| template function v.s. template class | George2 | C++ Programming | 3 | 12-13-2007 01:46 AM |
| error: template with C linkage | michaels-r | C++ Programming | 3 | 05-17-2006 08:11 AM |
| oh me oh my hash maps up the wazoo | DarkDays | C++ Programming | 5 | 11-30-2001 12:54 PM |
| bug for static functions in template class | Unregistered | C++ Programming | 3 | 09-16-2001 06:38 PM |