• Templates refuse to compile
    14 replies, posted
Hey there gents, I've been trying to get these to compile in a test (They compile fine on their own, but as soon as I create an instance of the class it shits itself), so some help would be appreciated. here's my code [b]Vector3.hpp[/b] [cpp] namespace Math { template <typename T> class Vector3 { public: Vector3(void); // Creates a blank Vector3 with value (0.0, 0.0, 0.0) Vector3(T x, T y, T z); // Constructor sets type to x, y, z ~Vector3(void); void X(T x); // Sets X to x void Y(T y); // Sets Y to y void Z(T z); // Sets Z to z T GetX(void); // Returns x T GetY(void); // Returns y T GetZ(void); // Returns z private: T x; T y; T z; }; } [/cpp] [b]Vector3.cpp[/b] [cpp] #include <Math/Vector3.hpp> namespace Math { template <typename T> Vector3<T>::Vector3(void) { this->x = 0; this->y = 0; this->z = 0; } template <typename T> Vector3<T>::Vector3(T x, T y, T z) { this->x = x; this->y = y; this->z = z; } template <typename T> Vector3<T>::~Vector3(void) { } /************************ Properties *******************/ /****** Set ******/ template <typename T> void Vector3<T>::X(T x) { this->x = x; } template <typename T> void Vector3<T>::Y(T y) { this->y = y; } template <typename T> void Vector3<T>::Z(T z) { this->z = z; } /****** Get ******/ template <typename T> T Vector3<T>::GetX(void) { return this->x; } template <typename T> T Vector3<T>::GetY(void) { return this->y; } template <typename T> T Vector3<T>::GetZ(void) { return this->z; } } [/cpp] and my main function: [cpp] #include <iostream> #include <Math/Vector3.hpp> int main(int argc, char* argv[]) { Math::Vector3<int>* vec3 = new Math::Vector3<int>(); int x = vec3->GetX(); std::cout << x << endl; } [/cpp] and results in THESE wonderful retarded errors [code] undefined reference to `Math::Vector3<int>::Vector3()' undefined reference to `Math::Vector3<int>::GetX()' [/code] I'm using g++. This fails in both windows and linux. so confused @_@. These are not in an external lib. any other questions I can answer.
Templated functions won't get linkage (they're not compiled to object form, more specifically, they can't be), so you need to declare [I]and[/I] define them in the header. This is because the environment for templated functions is deduced from two different contexts, the context where the function is defined, and the context where you instanciate the template.
[QUOTE=jA_cOp;16070081]Templated functions won't get linkage (they're not compiled to object form, more specifically, they can't be), so you need to declare [I]and[/I] define them in the header. This is because the environment for templated functions is deduced from two different contexts, the context where the function is defined, and the context where you instanciate the template.[/QUOTE] okay, so I will basically have the entire templated class in the header? that's fucking retarded. You hear that C++ You're templates feature is retarded. FUCK YOU. EDIT: It's working now, thanks man. Again, FUCK YOU templates.
That's how templates work in basically any compiled language. C++ has the "export" keyword which was supposed to fix this, but the standard didn't include any ideas on how to implement the feature. In reality, only one compiler supports "export", the commercial Comeau C++ compiler. It lets you disguise the process and define templated functions and classes in cpp files, but it doesn't actually compile the template instances to object code, so you still have to ship the source for the templates along with the rest of the code in object form.
When I used to mess with C/C++ this would always anger me as well... Good thing C# templates/generics don't behave like this. :)
[QUOTE=VoiDeD;16070695]When I used to mess with C/C++ this would always anger me as well... Good thing C# templates/generics don't behave like this. :)[/QUOTE] There are no generics in C.
What's the point of X() and GetX() over a simple "x" public member?
[QUOTE=VoiDeD;16070695]Good thing C# generics don't behave like this. :)[/QUOTE] Only because they're a lot more limited.
[QUOTE=jA_cOp;16070818]There are no generics in C.[/QUOTE] Oh right. :buddy: [QUOTE=gparent;16080792]Only because they're a lot more limited.[/QUOTE] I find template specialization more trouble than it's worth, and really only fits well in certain situations. This might be because I find C++ templates a little :bang: at times. It would, however, be interesting if C# 5.0+ brought specialization to the table, it would probably be a lot easier to understand, and you wouldn't get compilation errors like in the OP.
Well yeah, a version of C# released post-2009 running with JIT would probably handle it a lot better than a purely native language that started around 1983 ;)
[QUOTE=gparent;16084943]Well yeah, a version of C# released post-2009 running with JIT would probably handle it a lot better than a purely native language that started around 1983 ;)[/QUOTE] Version 1 of D was released in 2007, and it manages it a LOT better :)
[QUOTE=nullsquared;16076589]What's the point of X() and GetX() over a simple "x" public member?[/QUOTE] Well, any answer?
[QUOTE=jA_cOp;16092844]Version 1 of D was released in 2007, and it manages it a LOT better :)[/QUOTE] Which is exactly my point.
[QUOTE=nullsquared;16098058]Well, any answer?[/QUOTE] I was trying to make it more XNA like, but I've since changed it to be just a struct, with public members.
C# Generics kinda suck hard. D owns C# there.
Sorry, you need to Log In to post a reply to this thread.