Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 5th, 2007, 3:46 AM   #1
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
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!

rwm is offline   Reply With Quote
Old Sep 5th, 2007, 2:33 PM   #2
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
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.
Cache is offline   Reply With Quote
Old Sep 6th, 2007, 2:47 AM   #3
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
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 blah

I 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!
rwm is offline   Reply With Quote
Old Sep 6th, 2007, 3:00 AM   #4
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
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 App

Is it because i'm declaring static members in the header file?

Hope you can help!

Thanks!
rwm is offline   Reply With Quote
Old Sep 6th, 2007, 7:11 AM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old Sep 6th, 2007, 7:40 AM   #6
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
Hey Grumpy!

Thanks for the reply! Ok, i'll do that!

//static member declaritions

Is this not the right terminology?

Thanks!

rwm is offline   Reply With Quote
Old Sep 7th, 2007, 2:09 AM   #7
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
Well that fixed the problem! Thanks man!
rwm is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:38 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC