Thread: Error compiling a specialized template in Devc++

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    21

    Error compiling a specialized template in Devc++

    http://www.cplusplus.com/doc/tutorial/templates.html

    I tried to compile the example given on there about template specializations (just added a pause at the end), but the compiler doesn't seem to like it for some reason.

    Code:
    // template specialization
    #include <iostream>
    using namespace std;
    
    template <class T>
    class container {
        T element;
      public:
        container (T arg) {element=arg;}
        T increase () {return ++element;}
    };
    
    template <>
    class container <char> {
        char element;
      public:
        container (T arg) {element=arg;}
        char uppercase ();
    };
    
    template <>
    char container<char>::uppercase()
    {
      if ((element>='a')&&(element<='z'))
      element+='A'-'a';
      return element;
    }
    
    int main () {
      container<int> myint (7);
      container<char> mychar ('j');
      cout << myint.increase() << endl;
      cout << mychar.uppercase() << endl;
      
      system("pause");
      return 0;
    }
    Can someone please tell me what is wrong here?
    Does the compiler has problems with the empty <> on the template definition? Is this a compiler or code problem?

    Thank you in advance

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    (just added a pause at the end)
    That could be the problem. As far as I know, system() is in <cstdlib>.

    Where is the error?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I get two errors when I compile the code from the webpage. First, it should be
    Code:
    container (char arg) {element=arg;}
    in the specialization of the class instead of T, since T would be undeclared at that point.

    The second error is fixed when I remove the template<> from the implementation of uppercase. My compiler is VC++ 7.1. I'm not sure if the issues are due to our compilers or the cplusplus tutorial code.

    BTW, Comeau online gives the following errors (and compiles it cleanly with the changes I mentioned above):
    Code:
    Comeau C/C++ 4.3.3 (Aug  6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
    Copyright 1988-2003 Comeau Computing.  All rights reserved.
    MODE:strict errors C++
    
    "ComeauTest.c", line 17: error: identifier "T" is undefined
          container (T arg) {element=arg;}
                     ^
    
    "ComeauTest.c", line 22: error: "char container<char>::uppercase()" is not an
              entity that can be explicitly specialized
      char container<char>::uppercase()
                            ^
    
    2 errors detected in the compilation of "ComeauTest.c".

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Works fine using VC++6.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    Sigh... i guess i'll have to get another compiler.
    VC++6 = Visual Studio 6 ?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes, VC++ 6 is Visual Studio 6, which doesn't have great support for templates. VC 7.1 and Comeau are known for better template support and they don't compile that code either, so I would say it is a bug in the tutorial.

    Don't switch to Visual Studio 6 just because the tutorial is using incorrect code.
    Last edited by Daved; 02-28-2006 at 05:38 PM.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    Okay, I changed both things in the code and now it works... Is there a problem with template compatibility, or the code was just wrong?

    Thanks to everyone for the help
    Last edited by Kylecito; 02-28-2006 at 06:25 PM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't know for sure off the top of my head, but if VC 6 compiles it, chances are that the tutorial writer was just using the old compiler and the code is just wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM