Classes Using Templates

This is a discussion on Classes Using Templates within the C++ Programming forums, part of the General Programming Boards category; I'm new to using templates and they seem to be angry at me. I'm just assuming my problem is with ...

  1. #1
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034

    Classes Using Templates

    I'm new to using templates and they seem to be angry at me.

    I'm just assuming my problem is with the template, because I'm not new to classes and I cant see why my class would be spazzing on me (and this isnt all of the code either).

    Code:
    #include <iostream>
    
    typedef unsigned short int USHORT;
    
    template< class Datatype >
    class Array
    {
    private:
      Datatype* m_array;
      USHORT m_size;
    
    public:
      Array (USHORT);
      ~Array ();
    
    };
    
    Array::Array (USHORT p_sizeInitial)
    {
      m_array = new Datatype[p_sizeInitial];
      m_size = p_sizeInitial;
    }
    
    Array::~Array ()
    {
      if (m_array != 0)
        delete[] m_array;
    
      m_array = 0;
    }
    I get an error before :: in the constructor definition, and a bunch of places afterwards. Do I have to declare the template before functions of the class too? or put the class declaration in a .h and put definitions in the .cpp?

    Whats going on!
    Warning: Have doubt in anything I post.

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

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    The easiest thing to do is to put all the code inside the class definition as if you wanted all these functions implicitly inlined. Or alternatively add the template arguments to the function definitions.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Stoned_Coder
    The easiest thing to do is to put all the code inside the class definition as if you wanted all these functions implicitly inlined. Or alternatively add the template arguments to the function definitions.
    Wow, thats um.. gay, well thanks for the quick reply

    Edit:

    Code:
    template< class Datatype >
    Array::Array (USHORT p_sizeInitial)
    {
      m_array = new Datatype[p_sizeInitial];
      m_size = p_sizeInitial;
    }
    
    template< class Datatype >
    Array::~Array ()
    {
      if (m_array != 0)
        delete[] m_array;
    
      m_array = 0;
    }
    Is that how you meant? because it isnt working... nor is adding the template part to the declarations in the class.

    My book says its because templates are relatively new to the standard and most compilers have a flaw when it comes to using them, the whole fact that it will give errors unless you declare and impliment the functions in, as if they were inline, inside the class.

    The author found a way around it in MSVC6, by defining the methods inline. This doesnt seem to be working in Dev-Cpp, so I'll be switching compilers now..
    Last edited by Dae; 07-14-2005 at 05:20 AM.
    Warning: Have doubt in anything I post.

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

  4. #4
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    Code:
    template< class Datatype >
    Array< Datatype >::Array (USHORT p_sizeInitial)
    {
      m_array = new Datatype[p_sizeInitial];
      m_size = p_sizeInitial;
    }
    :wq

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    This doesnt seem to be working in Dev-Cpp, so I'll be switching compilers now.
    Try viaxd's suggestion, the compiler used in Dev-C++ is more standards compliant than that used in MSVC6.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    You don't have to define the methods as inline, you just have to put the methods in the header file (it is 'bad form' to include the cpp file). The vast majority of compilers will only look in included files (i.e., headers) and will not willy nilly root around looking for suitable source files to find what they need. Some compilers will (Intel's compiler will actually modify the binary and do inlining at that point). I have used templates in DevC with no problem, no need to switch compilers.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  7. #7
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by mitakeet
    You don't have to define the methods as inline, you just have to put the methods in the header file (it is 'bad form' to include the cpp file).
    My style is to have the class with the method declarations inside of a .h, include that .h into the .cpp file, and the .cpp file part of the Project List (or makefile). I dont plan on moving the methods inside the class, or putting them inside the .h file. The author said to get around the errors of seperating the methods declaration (in the .h) and the methods code (in the .cpp) is to define them as inline.. which is perfect for the way I want my class laid out.

    viaxd suggestion is also perfect for how I want it laid out. And apparently works with Dev-Cpp, so thats cool.. I wonder if MSVC6 complains. I'll check and see which way I'll use, I'd prefer getting into a more portable habit, too bad it ranges from compiler to compiler, and doesnt make sense in the first place (all that template defining, should be handled by the compiler).

    Thanks for the suggestions guys!
    Warning: Have doubt in anything I post.

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

  8. #8
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    It depends on how deep into templates you get (template specialization, default template parameters, etc), but you shouldn't encounter any problems with plain old templates in most compilers.

    Side note, regarding style, I personally prefer to use the keyword "typename" instead of "class" in template parameter lists. Consider it.

  9. #9
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by mitakeet
    You don't have to define the methods as inline, you just have to put the methods in the header file (it is 'bad form' to include the cpp file). The vast majority of compilers will only look in included files (i.e., headers) and will not willy nilly root around looking for suitable source files to find what they need. Some compilers will (Intel's compiler will actually modify the binary and do inlining at that point). I have used templates in DevC with no problem, no need to switch compilers.
    He didnt say inline them at all. He said put the definitions directly in the class. Although its fine with small code, it could be a hastle with larger code.

  10. #10
    *this
    Join Date
    Mar 2005
    Posts
    498
    Wow now im curious, So I have this:
    Code:
    #include <iostream>
    using namespace std;
    
    template <typename type>
    class Foo {
       public:
          Foo (type A) { cout << A; };
       private:
    };
    
    int main () {
       Foo<int> bar(5);
       
       cin.get();
       return 0;
    }
    It compiles fine and works but there is no way to move the functions out without making them each a template...You sure there isnt a way to do it?

  11. #11
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by JoshR
    He didnt say inline them at all. He said put the definitions directly in the class. Although its fine with small code, it could be a hastle with larger code.
    Stoned_Coder said to put the definitions directly in the class, and I said inline the declarations. This was suggested by an author of a game programming book in order to avoid the errors caused by MSVC6 when having templated classes and wanting the methods to be declared inside the class but the code to be outside of it.

    The equivalent of using 'inline' keyword for MSVC6 would be to template each method as suggested above:

    Code:
    template< class Datatype >
    Array< Datatype >::Array (USHORT p_sizeInitial)
    {
      m_array = new Datatype[p_sizeInitial];
      m_size = p_sizeInitial;
    }
    It compiles fine and works but there is no way to move the functions out without making them each a template...You sure theres no way?
    Theres 2 ways: using the code above this quote, and for MSVC6 you can declare the method as inline.

    Oh and alright, I'll switch to typename, that does make more sense.
    Warning: Have doubt in anything I post.

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

  12. #12
    *this
    Join Date
    Mar 2005
    Posts
    498
    you don't necessarily have to use typename, it just feels weird using class lol. Ok, so I guess you cant use a templated class and move the functions outside of the class.

  13. #13
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    He didnt say inline them at all. He said put the definitions directly in the class.
    Implementing a function within it's class is the same as declaring it inline.

  14. #14
    *this
    Join Date
    Mar 2005
    Posts
    498
    Ya but you dont necessarily have to do it because its not gauranteed to be inlined

  15. #15
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by JoshR
    you don't necessarily have to use typename, it just feels weird using class lol.
    Yeah, I know, hes right though, using typename makes more sense to use than class when refering to style, which is why he said "speaking of style".

    Quote Originally Posted by JoshR
    Ok, so I guess you cant use a templated class and move the functions outside of the class.
    Dude, someone already provided a way to do that, which I echoed in the last post:

    Code:
    class Array
    {
    public:
      Array(USHORT);
    }
    
    template< class Datatype >
    Array< Datatype >::Array (USHORT p_sizeInitial)
    {
      m_array = new Datatype[p_sizeInitial];
      m_size = p_sizeInitial;
    }
    Also in MSVC6 you can declare the methods as inline, and move the functions outside of the class (which the author of Game Structures for Game Programmers says):

    Code:
    class Array
    {
    public:
      inline Array(USHORT);
    }
    
    Array::Array (USHORT p_sizeInitial)
    {
      m_array = new Datatype[p_sizeInitial];
      m_size = p_sizeInitial;
    }
    Warning: Have doubt in anything I post.

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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulating Templates or Classes in C
    By algatt in forum C Programming
    Replies: 1
    Last Post: 04-09-2007, 02:12 PM
  2. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  4. VC++6: exporting classes containing templates
    By ttt in forum Windows Programming
    Replies: 2
    Last Post: 09-15-2003, 11:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21