Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jul 11th, 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
template doesnt compile

this compiles fine with GCC, but not with visual studio 2005...

im trying to pass a default value to a template class

i.e.

myclass<float,1.0f>

#include <iostream>
using namespace std;

template<typename T,const T TUNIT>
class TClass {
	public:
		TClass() : data(TUNIT) {}
		TClass(const T &t) : data(t) {}
		T get_data() {
			return data;
		}
	private:
		T data;
};

int main() {
	//
	TClass<int,1> tc;
	cout << tc.get_data() << endl;

	return 0;
}

any idea why? i should have copied the error message from home
rwm is offline   Reply With Quote
Old Jul 11th, 2007, 3:56 AM   #2
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
i dont understand!

this works

TClass<int,1> tc;

but this doesnt

TClass<float,3.142f> tc;

heres the error:

test.cpp:18: error: ‘float’ is not a valid type for a template constant parameter
test.cpp:18: error: invalid type in declaration before ‘;’ tok



pretty much the same error i get with visual studio...
rwm is offline   Reply With Quote
Old Jul 11th, 2007, 7:27 AM   #3
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
Just as the error states you can't use a float as a non-type template parameter unfortunately, see http://publib.boulder.ibm.com/infoce...parameters.htm for example.
__________________
Visit my website BinaryNotions.
Eoin is offline   Reply With Quote
Old Jul 11th, 2007, 8:10 AM   #4
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
arrrrrrrrrrgghhhhhhhh why?

im trying to pass values to the template so i can initialize properly..

i.e.

template<typename T,T TZERO,T TUNIT>
class TClass {
    public:
        TClass() : zero(TZERO),unit(TUNIT) {}
        ...
    private:
        //data
        T zero;
        T unit;
};

is there any way to do this?

thanks for the reply!
rwm is offline   Reply With Quote
Old Jul 11th, 2007, 10:38 AM   #5
Oxyd
Newbie
 
Oxyd's Avatar
 
Join Date: Dec 2004
Location: Czech Republic
Posts: 1
Rep Power: 0 Oxyd is on a distinguished road
Send a message via ICQ to Oxyd
Hm, no. Template parameter can only be a type or an integral constant -- floats are simply not allowed. You could pass a default value to the constructor, though:
template <typename T>
class TClass {
  public:
    TClass(T zero, T unit) : zero(zero), unit(unit) { }

  private:
    T const zero;
    T const unit;
};
Oxyd is offline   Reply With Quote
Old Jul 11th, 2007, 10:42 AM   #6
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
To be honest off hand I'm not sure. You might be able to support floats as a fraction of two integers but that may not be allowed.
__________________
Visit my website BinaryNotions.
Eoin is offline   Reply With Quote
Old Jul 11th, 2007, 10:45 AM   #7
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
ok thanks for the help! bit of a strange restriction right?

EDIT: only saw your post Eoin, ah but thats tricky buggery and id like to avoid it...

would be nice to put in

TClass<float,1.0f> tc1;
TClass<double,1.0> tc2;

etc
rwm is offline   Reply With Quote
Old Jul 11th, 2007, 10:54 AM   #8
francoissoft
Newbie
 
Join Date: Jul 2007
Location: Ohio
Posts: 19
Rep Power: 0 francoissoft is on a distinguished road
Quote:
Originally Posted by rwm View Post
ok thanks for the help! bit of a strange restriction right?

EDIT: only saw your post Eoin, ah but thats tricky buggery and id like to avoid it...

would be nice to put in

TClass<float,1.0f> tc1;
TClass<double,1.0> tc2;

etc
Not really, templates are macros based on specified data types, therefore a template can only take parameters that are data types.

Check out http://www.cplusplus.com for more details.
francoissoft is offline   Reply With Quote
Old Jul 12th, 2007, 2:39 AM   #9
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
well i found a way, its feels pretty much like a hack but works

template<typename T>
class TClass {
        public:
                TClass() : data(TUNIT) {} //init data
                ...
        private:
                //static members - internal use only
                static const T TUNIT;
                //data
                T data;
};

template<> const float TClass<float>::TUNIT(1.0f);

works fine, so should be content then!
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 final step bl00dninja C++ 7 May 11th, 2007 12:05 AM
AVL delete function bl00dninja C++ 1 May 4th, 2007 9:04 AM
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
Template + operator problem Polyphemus_ C++ 3 Sep 30th, 2005 6:43 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:01 AM.

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