MSVC 7.1: Template specialisation??

This is a discussion on MSVC 7.1: Template specialisation?? within the C++ Programming forums, part of the General Programming Boards category; I'm fiddling with template specialisation (it's been on my list of things to do for weeks) and found a little ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    MSVC 7.1: Template specialisation??

    I'm fiddling with template specialisation (it's been on my list of things to do for weeks) and found a little tutorial on cplusplus.com but MSVC 7.1 doesn't seem to like it:

    Code:
    template< typename T > class Container
    {
    public:
        Container( T& val )
        {
            m_Value = val;
        }
    
        ~Container()
        {}
    
        void    Increment();
    
    private:
        T   m_Value;
    };
    
    template< typename T > void Container< T >::Increment()
    {
        ++m_Value;
    }
    
    // Specialisation of Container class
    template<> class Container< char >
    {
    public:
        Container( T& val )
        {
            m_Value = val;
        }
    
        ~Container()
        {}
    
        void    Uppercase();
    
    private:
        char    m_Value;
    };
    
    template<> void Container< char >::Uppercase()
    {
        return toupper(m_Value);
    }
    Am I doing something wrong? I thought it was just MSVC 5 that didn't like this.

    EDIT: The errors:

    Code:
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(122) : error C2143: syntax error : missing ')' before '&'
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(122) : error C2143: syntax error : missing ';' before '&'
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(122) : error C2460: 'Container<char>::T' : uses 'Container<char>', which is being defined
            c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(134) : see declaration of 'Container<char>'
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(122) : error C2059: syntax error : ')'
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(122) : error C2501: 'Container<char>::val' : missing storage-class or type specifiers
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(124) : error C2065: 'val' : undeclared identifier
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(124) : error C2864: 'm_Value' : only const static integral data members can be initialized inside a class or struct
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(124) : error C2501: 'Container<char>::m_Value' : missing storage-class or type specifiers
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(127) : error C2588: '::~Container' : illegal global destructor
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(132) : error C2059: syntax error : 'private'
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(134) : error C2059: syntax error : '}'
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(134) : error C2143: syntax error : missing ';' before '}'
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(134) : error C2059: syntax error : '}'
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(136) : error C2143: syntax error : missing ';' before '<'
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(136) : error C2182: 'Container' : illegal use of type 'void'
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(136) : error C2373: 'Container' : redefinition; different type modifiers
            c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(127) : see declaration of 'Container'
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(136) : error C2988: unrecognizable template declaration/definition
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(136) : error C2059: syntax error : '<'
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(142) : error C2143: syntax error : missing ';' before '{'
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(142) : error C2447: '{' : missing function header (old-style formal list?)
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Just a guess...is line 122 this line:
    Code:
    template<> class Container< char >
    {
    public:
        Container( T& val ) //line 122?
        {
    You should probably change the T to char. Beyond that, I'm not sure.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Tried it earlier and it didn't make a difference IIRC (too busy to check right now).
    I'm not likely to use it very much so I'll moan about it again when if I do
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    All right...weekend's over, so I got my C++ compiler back. This compiled on 8.0; I can't guarantee that it'll work for 7.1.
    Code:
    // Specialisation of Container class
    template<> class Container< char >
    {
    public:
        Container( char& val )
        {
            m_Value = val;
        }
    
        ~Container()
        {}
    
        int    Uppercase();
    
    private:
        char    m_Value;
    };
    
    //You can't specialize twice.  The above declaration
    //already says that this function is specialized.
    int Container< char >::Uppercase()
    {
        return toupper(m_Value);
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    @alhuka: cplusplus.com is a good reference sometimes, but the code in the tutorials are often fishy. You're better off learning somewhere else.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Coversion operator template specialisation
    By DL1 in forum C++ Programming
    Replies: 8
    Last Post: 01-20-2009, 04:14 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  4. Template Friend Workaround for MSVC?
    By LuckY in forum C++ Programming
    Replies: 1
    Last Post: 04-01-2005, 05:41 PM
  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

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