![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
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 ![]() |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
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... |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
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! |
|
|
|
|
|
#5 |
|
Newbie
|
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;
}; |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
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 |
|
|
|
|
|
#8 | |
|
Newbie
Join Date: Jul 2007
Location: Ohio
Posts: 19
Rep Power: 0
![]() |
Quote:
Check out http://www.cplusplus.com for more details. |
|
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
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! ![]() |
|
|
|
![]() |
| 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 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 |