Class Template Trouble

This is a discussion on Class Template Trouble within the C++ Programming forums, part of the General Programming Boards category; I am trying to implement a class template called Array to use with different types, the declaration looks like this: ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    21

    Class Template Trouble

    I am trying to implement a class template called Array to use with different types, the declaration looks like this:

    Code:
    #ifndef _ARRAY_HPP_
    #define _ARRAY_HPP_
    
    #include <iostream>  // for cout, cin
    #include <exception> // for exception base class
    #include <new>       // for bad_alloc
    
    template <typename Type>
    class Array
    {
    private:
    	// Holds the size of the array
    	unsigned len;
    
    	// Pointer for dynamically allocated array
    	Type * array;
    
    public:
    	// Constructor with array size explicitly provided
    	explicit Array(unsigned arraySize = 0);
    
    	// Homogeneous copy constructor
    	Array(const Array<Type> & ST_Array);
    	
    	// Heterogeneous copy constructor, can take a different type of array as argument
    	template <typename U>
    	Array(const Array<U> & DT_Array);
    
    	// Overloaded bracket operator const version
    	const Type & operator[](unsigned i) const;
    
    	// Overloaded bracket operator non-const version
    	Type & operator[](unsigned i);
    
    	// Homogeneous assignment operator
    	Array<Type> & operator=(const Array<Type> & a);
    
    	// Heterogeneous assignment operator
    	template <typename U>
    	Array<Type> & operator=(const Array<U> & a);
    
    	// Returns the size of the array
    	unsigned Size(void) const;
    
    	// Destructor
    	~Array(void);
    
    };
    
    
    
    // ***********Implementation************										
    
    template <typename Type>
    Array<Type>::Array(unsigned arraySize) : len(arraySize)
    {
    	array = new Type[len];
    }
    
    // Returns the size of the array
    template <typename Type>
    unsigned Array<Type>::Size(void) const
    {
    	return this->len;
    }
    
    // Homogeneous copy constructor
    template <typename Type>
    Array<Type>::Array(const Array<Type> & ST_Array)
    {
    	len = ST_Array.len;
    	array = new Type[len];
    	for (unsigned i = 0; i < len; i++)
    		array[i] = ST_Array[i];
    }
    
    // Heterogeneous copy constructor, can take a different type of array as argument
    template <typename Type>
    template <typename U>
    Array<Type>::Array(const Array<U> & DT_Array)
    {
    	len = DT_Array.Size();
    	array = new Type[len];
    	for (unsigned i = 0; i < len; i++)
    		array[i] = Type(DT_Array[i]);
    }
    
    // Overloaded bracket operator const version
    template <typename Type>
    const Type & Array<Type>::operator[](unsigned i) const
    {
    	return array[i];
    }
    
    // Overloaded bracket operator non-const version
    template <typename Type>
    Type & Array<Type>::operator[](unsigned i)
    {
    	return array[i];
    }
    
    // Homogeneous assignment operator
    template <typename Type>
    Array<Type> & Array<Type>::operator=(const Array<Type> & a)
    {
    	if (this != &a)
    	{
    		delete [] array;
    		array = new Type[a.len];
    		for (unsigned i = 0; i < len; i++)
    			array[i] = a[i];
    	}
    	return *this;
    }
    
    
    // Heterogeneous assignment operator
    template <typename Type>
    template <typename U>
    Array<Type> & Array<Type>::operator=(const Array<U> & a)
    {
    	delete [] array;
    	len = a.Size();
    	array = new Type[len];
    	for (unsigned i = 0; i < len; i++)
    		array[i] = Type(a[i]);
    	return *this;
    }
    
    
    // Destructor
    template <typename Type>
    Array<Type>::~Array(void)
    {
    	delete [] array;
    }
    
    #endif
    and my driver is very simple right now just
    Code:
    #include <iostream>
    #include "Array.hpp"
    
    using namespace std;
    
    int main(void)
    {
    	Array<float> a(1);
    	return 0;
    }
    But when I ran it I got this message
    Code:
    C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xstring(32): fatal error C1001: INTERNAL COMPILER ERROR
            (compiler file 'msc1.cpp', line 2701) 
             Please choose the Technical Support command on the Visual C++ 
             Help menu, or open the Technical Support help file for more information
    I really can't see where I am doing wrong, please help. Thanks.
    Last edited by pliang; 04-21-2005 at 03:53 AM.

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Start by correcting following errors:
    Size returns unsigned so that line should be:
    Code:
    unsigned Array<Type>::Size(void)
    also DT_Array is not same as DT_array
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    Yay, it works now. I knew it was something stupid like that, thanks a lot Micko! I got another quest though, why do I have to make my

    unsigned Size(void) const;

    a const member function? I was getting error from my copy constructor when I didn't put the const at the end.

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    I'm not sure because this works for me:
    Code:
    //dec....
    	// Returns the size of the array
    	unsigned Size(void);
    //imp...
    template <typename Type>
    unsigned Array<Type>::Size(void) 
    {
    	return this->len;
    }
    But if you try something like this you'll get a compiler error:
    Code:
    const Array<float> b(a);
    b.Size();
    const function means that function is not allowed to change object and that's it.
    It's not a bad practice do declare/define functions that don't change object to const...

    - Micko
    Last edited by Micko; 04-21-2005 at 04:13 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You can only call const methods on a const object. Thefore, if none of your methods are const, a const object is completely useless. As copy constructors and similar functions are passed references to const objects, the objects must have some const functions. Size(), since it doesn't change the object, is a perfect candidate for this.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Template class nested in template class
    By yahn in forum C++ Programming
    Replies: 7
    Last Post: 04-18-2009, 12:40 PM
  3. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 11:17 AM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Declare a template class as a friend?
    By AH_Tze in forum C++ Programming
    Replies: 11
    Last Post: 05-19-2004, 10:24 PM

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