Thread: Templates

  1. #1
    Newbie Programmer
    Join Date
    Oct 2005
    Location
    Montreal, Canada
    Posts
    5

    Unhappy Templates

    I just wrote a very simple vector class using templates:
    Code:
    template <typename Type> class Vector
    {
    public:
        Type X;
        Type Y;
        Type Z;
    };
    But how do I define functions and constructors like Vector(Type, Type, Type)? I get compile errors whenever I try. Any help would be appreciated.

  2. #2
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Code:
    template <typename Type>
    Vector<Type>::Vector() {}
    
    template <typename Type>
    Type Vector<Type>::GetX() { return X; }
    Or simply put them inside the class itself, which is usually best because templated classes dont function as well as normal classes do.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Newbie Programmer
    Join Date
    Oct 2005
    Location
    Montreal, Canada
    Posts
    5
    This gives an error message:
    Vectors error LNK2019: unresolved external symbol "public: __thiscall Vector<float>::Vector<float>(void)" (??0?$Vector@M@@QAE@XZ) referenced in function _main

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Start a new project with just the following code, see if that compiles. It should compile without any problems.
    Code:
    template <typename Type> class Vector {
    public:
    	Vector(Type,Type,Type);
    	Type GetX();
    private:
        Type X;
        Type Y;
        Type Z;
    };
    
    template <typename Type>
    Vector<Type>::Vector(Type x, Type y, Type z) {
    	X=x; Y=y; Z=z;
    }		
    
    template <typename Type>
    Type Vector<Type>::GetX() { return X; }
    
    #include <iostream>
    using namespace std;
    
    int main() {
    	Vector<int> myVect(1,2,3);
    	cout << myVect.GetX();
    }

  5. #5
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    one thing to remember is that when you create a template class, most compilers will not let you put the function definitions in a separate .cpp file, everything will need to be in the header file.

  6. #6
    Newbie Programmer
    Join Date
    Oct 2005
    Location
    Montreal, Canada
    Posts
    5
    Quote Originally Posted by rockytriton
    one thing to remember is that when you create a template class, most compilers will not let you put the function definitions in a separate .cpp file, everything will need to be in the header file.
    Oh, so that's the problem. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 01:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM