![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
static template members
Hey!
I had some problems with static template members: // TClass.hpp
#pragma once
namespace Global
{
namespace A
{
template<typename T>
class TClass
{
public:
TClass() {}
TClass(const T &t) : data(t) {}
//static member
static const TClass<T> Default;
private:
//data
T data;
};
//static member declaritions
template<> const TClass<float> TClass<float>::Default(1.0f);
template<> const TClass<double> TClass<double>::Default(1.0);
};
};As you can see this is nested in two namespaces, Global and A. I've defined a header file: // Types.hpp
#pragma once
#include "TClass.hpp"
namespace Global
{
namespace A
{
typedef TClass<float> Class;
};
};Which I use in another class, defined in a different namespace: //SomeClass.hpp
#pragma once
#include "Types.hpp"
using namespace Global::A;
namespace Global
{
namespace B
{
class SomeClass
{
//uses Class which is typedef of template<typename T> TClass
};
};
};I get compiler errors for every static member declared saying that the data was declared more than once? How is that possible? Hope you can help! Thanks! ![]() |
|
|
|
|
|
#2 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
Works in VC8 (MSVC 2005) with no errors. Perhaps the 'once' pragma is meaningless in your environment? BTW, C++ doesn't require a semicolon to terminate a namespace.
|
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
Hey Cache!
Thanks for the reply! Mmm, well that's not exactly the code, but very much along the lines of that... I've forgotten to bring the source from home, was in such a rush! But it was along the lines of that... As for semi-colon at end of namespace brace, I do that purposely. I don't know why, I kinda like it... And always add a comment to end as well namespace blah
{
...
}; //namespace blahI might have an old backup on my flash disk, i'll have a look, if i find it i'll compile it and post the errror! |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
Found it.
Hey, here's the exact error: /tmp/cc1uTVHE.o(.bss+0x3d0): multiple definition of `Mathematics::TVector<double>::kZero' /tmp/ccDuSaWS.o(.bss+0x3d0): first defined here /tmp/cc1uTVHE.o(.bss+0x3e8): multiple definition of `Mathematics::TVector<double>::kZAxis' /tmp/ccDuSaWS.o(.bss+0x3e8): first defined here /tmp/cc1uTVHE.o(.bss+0x400): multiple definition of `Mathematics::TVector<double>::kYAxis' /tmp/ccDuSaWS.o(.bss+0x400): first defined here /tmp/cc1uTVHE.o(.bss+0x418): multiple definition of `Mathematics::TVector<double>::kXAxis' /tmp/ccDuSaWS.o(.bss+0x418): first defined here /tmp/cc1uTVHE.o(.bss+0x430): multiple definition of `Mathematics::TVector<float>::kZero' /tmp/ccDuSaWS.o(.bss+0x430): first defined here /tmp/cc1uTVHE.o(.bss+0x43c): multiple definition of `Mathematics::TVector<float>::kZAxis' /tmp/ccDuSaWS.o(.bss+0x43c): first defined here /tmp/cc1uTVHE.o(.bss+0x448): multiple definition of `Mathematics::TVector<float>::kYAxis' /tmp/ccDuSaWS.o(.bss+0x448): first defined here /tmp/cc1uTVHE.o(.bss+0x454): multiple definition of `Mathematics::TVector<float>::kXAxis' From this source: #pragma once
//local dependencies
#include "Config.hpp"
#include "Types.hpp"
namespace App
{
namespace Mathematics
{
//forward declaritions
template<typename T> class TMath;
template<typename T> class TPoint;
template<typename T>
class ITEM TVector
{
public:
//...
//static members
ITEM static const TVector<T> kXAxis;
ITEM static const TVector<T> kYAxis;
ITEM static const TVector<T> kZAxis;
ITEM static const TVector<T> kZero;
private:
//data
T vector[3];
protected:
//
}; //class TVector
//static member declaritions
//single precision
template<> const TVector<Float> TVector<Float>::kXAxis(1.0f,0.0f,0.0f);
template<> const TVector<Float> TVector<Float>::kYAxis(0.0f,1.0f,0.0f);
template<> const TVector<Float> TVector<Float>::kZAxis(0.0f,0.0f,1.0f);
template<> const TVector<Float> TVector<Float>::kZero(0.0f,0.0f,0.0f);
//double precision
template<> const TVector<Double> TVector<Double>::kXAxis(1.0,0.0,0.0);
template<> const TVector<Double> TVector<Double>::kYAxis(0.0,1.0,0.0);
template<> const TVector<Double> TVector<Double>::kZAxis(0.0,0.0,1.0);
template<> const TVector<Double> TVector<Double>::kZero(0.0,0.0,0.0);
///members go here
}; //namespace Mathematics
}; //namespace AppIs it because i'm declaring static members in the header file? Hope you can help! Thanks! ![]() |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5
![]() |
Yes, it is because you're defining the statics in the header file (or, more accuratly, because you're defining them in the header and then including that header in multiple source files [aka non-header files]).
Move the definitions (which are after the comment that says "//static member declaritions" (sic)) so they only appear in a single source file. |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
Hey Grumpy!
Thanks for the reply! Ok, i'll do that! //static member declaritions Is this not the right terminology? Thanks! ![]() |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
Well that fixed the problem! Thanks man!
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| avl trees again | bl00dninja | C++ | 0 | May 3rd, 2007 3:07 AM |
| friends, templates, and other s**t | bl00dninja | C++ | 4 | Oct 14th, 2006 1:15 AM |
| Initialization of static members | earl | C++ | 11 | Jul 29th, 2005 6:30 PM |
| static class members and libraries (linking question) | earl | C++ | 2 | Jun 22nd, 2005 5:21 PM |
| Multiple server clients using sockets + Threads | captainK | Java | 3 | Mar 4th, 2005 10:10 AM |