Thread: template class ..

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    11

    template class ..

    Hello All,

    Can someone pls explain where i'm going wrong?:
    here's a snippet of what i've written :

    Matrix.h ***********************************
    Code:
    template <typename T>
    class MTX{
          public:
                 /* Constructors */
                 MTX(int, int);  
          .............
    ******************************************
    Matrix.cpp**********************************
    Code:
    template <typename T> MTX<T>::MTX(int n, int m){
             A = new std::vector<std::vector<T> >(n, m);             
    }
    ******************************************
    main.cpp***********************************
    Code:
    main(){
          MTX<int> mtx(50,50); 
           
    }
    ******************************************


    i've put my includes in both main.cpp and Matrix.cpp (#include "Matrix.h")
    and when i compile DevCpp gives ma the following error:

    "[Linker error] undefined ref. to MTX<int>::MTX(int, int)"

    I really don't see what's going wrong(i know what linker error means btw) because i'm
    copying straight from an example in a book.

    Any help would be great as its my first time trying to make a template class.

    Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Templates are tricky creatures. Try putting your definition (the .cpp file contents) into the header file and it should compile.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I would highly advise against using a vector of vector<T>'s since it could end up being quite confusing. It's confusing just to look at it let alone use it.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Thanks for the suggestions


    1 - I tried putting the .ccp stuff in the .h file and i get the same error.

    2- What would you suggest other than using vector<T> ? I'm using it to try and make the
    class a generic as possible so i can do double and float math.


    Thanks again.

    Any more suggestions?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Notes:
    - Main should return int.
    - Main should have "return 0;" at the end.
    You can also inline your templates:
    Code:
    template<typename T> class foo()
    {
    	foo(int i, int j)
    	{
    		i = j; // Or whatever here
    	}
    };

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    See this FAQ for more information, especially starting with 35.12: http://www.parashift.com/c++-faq-lite/templates.html

    I don't see the problem with std::vector<std::vector<T> >. However, I wouldn't make A a pointer allocated with new. There's no point to doing that. Just make it a regular object.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Does your book actually show the template split up into a .h & .cpp file? That's bad. Most compilers barf when you do that, so everything should be in the .h file.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's still possible to put both declaration and definition in the header file, too.
    Your can make the usual declaration of the class and after that you can also define the class like you would in a .cpp file, but in the header. It makes the declaration much cleaner, so I do recommend it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template and friend class
    By black_spot1984 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2008, 05:50 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  4. Instantiating a template class
    By NullS in forum C++ Programming
    Replies: 11
    Last Post: 02-23-2005, 10:04 AM
  5. Function template in Class template?
    By Aidman in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2003, 09:50 AM