CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: template

  1. #1
    Join Date
    Jan 2011
    Posts
    15

    template

    In vc++, If I comment out the lines in main, any error such as an extra letter in definition of "void array<T>::setArray", is ignored
    by the compiler/linker. why?.

    Code:
    int main() 
    {
    array< int > int_array(5);
    int_array.setArray(0,1);
    return 0;
    }
    
    #ifndef ARRAY_H_
    #define ARRAY_H_
    
    template< class T > class array {
    private:
            int size;
            T *arrayA;
    public:
            array (int s) {
            sz = s;
            arrayA = new T [sz];
            }
    
    void setArray ( int index, T val);
    
    };
    
    template< class T >
    void array<T>::setArray ( int index, T val) {
            arrayA[index] = val; 
            }
    
    #endif

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: template

    First, be aware that there is a std::array class which your class may conflict with. (Although that's a fixed-size array; you seem to be attempting to create a dynamic array class more similar to std::vector.)

    To answer your question, compilers are not required to do anything at all with templates that are not instantiated. They may not be able to detect syntax errors in that case.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: template

    Why is your main() program defined before the template?

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: template

    Quote Originally Posted by BilJ0 View Post
    In vc++, If I comment out the lines in main, any error such as an extra letter in definition of "void array<T>::setArray", is ignored
    by the compiler/linker. why?.

    Code:
    int main() 
    {
    array< int > int_array(5);
    int_array.setArray(0,1);
    return 0;
    }
    
    #ifndef ARRAY_H_
    #define ARRAY_H_
    
    template< class T > class array {
    private:
            int size;
            T *arrayA;
    public:
            array (int s) {
            sz = s;
            arrayA = new T [sz];
            }
    
    void setArray ( int index, T val);
    
    };
    
    template< class T >
    void array<T>::setArray ( int index, T val) {
            arrayA[index] = val; 
            }
    
    #endif
    There are sometimes good reasons to do it, but these are rare occasions. So, here is my question... why are you trying to implement a vector like container class when you already have std::vector at you finger tips? Is it a learning exercise? If so, you can potentially learn quite a lot. If not, are you sure you really need to do it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured