CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2013
    Posts
    75

    Template error help

    Hi guys.

    I'm getting a template error.

    Error
    Code:
    /data/data/com.n0n3m4.droidc/files/temp.c:92:3: error: invalid use of template-name 'Array' without an argument list
       Array::Array(int s): size(s)
       ^
    compilation terminated due to -Wfatal-errors.
    Code:
    Code:
    // headers
    #include <iostream>
    #include <utility>
    #include <cctype>
    
    // stuff we need from namespace std
    using std::cout;
    using std::cin;
    using std::endl;
    using std::move;
    using std::isalpha;
    
    //exception class
    class InvalidSize {};
    class InvalidInput {};
    // Array class
    template <typename T>
    class Array
    {
    	public:
    	//constructors
    	Array()=delete; // Array must have a size, and cannot be stored in a structure.
    	Array(int size); //Throws InvalidSize 
    	//copy constructor
    	Array(const Array& rhs);
    	//move constructor
    	Array(Array&& rhs);
    	//conversion from regular array
    	Array(T* array, int size);
    	// copy assignment operator
    	Array& operator=(const Array& rhs);
    	//move assignment operator
    	Array& operator=(Array&& rhs);
    	//destructor
    	~Array();
    	//index operator
    	T& operator[](int index) const; 
    	//conversion to regular array
    	operator T*() const;
    	//get length of array
    	int getsize() const;
    	//data fields
    	private:
    	int size;
    	T* array;
    };
    
    //Thrower function for input validation
    void IsInputValid(bool condition);
    
    //main
    int main()
    {
    	//ask for array size
        cout << "How big do you want your array to be?" << endl;
        try
        {
        	//array size
        	int size;
        	//validate input
        IsInputValid(cin >> size);
        //construct array
        	Array<int> array(size);
        // get data
        cout << "Enter the numbers you'd like to store: " << endl;
        for (int i = 0; i < array.getsize(); i++)
        {
        	int val;
        	IsInputValid(cin >> val);
        	array[i]=val;
        }
        //display array contents
        cout << "Displaying array contents." << endl;
        for (int i=0; i < array.getsize(); i++)
        cout << array[i] << endl;
        }
        //handle exceptions
          catch (InvalidSize)
        {
        	cout << "Arrays must have at least two objects!" << endl;
        }
        catch (InvalidInput)
        {
        	cout << "Integers only!" << endl;
        }
     //done
     return 0;
    }
    
    //regular constructor
       template <typename T>
      Array::Array(int s): size(s)
      {
      	// make sure array has at least two objects
      	 if (s < 2)
      	throw InvalidSize();
      	//otherwise build the array
      	else
      	{
      		cout << "Building array." << endl;
      		array = new T[size];
      	}
      }
      
    //copy constructor
    Array::Array(const Array& rhs):size(rhs.size), array(new int[size])
    {
    	//delegate to copy assignment operator
    	*this = rhs;
    } 
    
    //move constructor
    Array::Array(Array&& rhs):array(0)
    {
    	//delegate to move assignment operator
    	*this = move(rhs);
    }
    
    //array conversion constructor
    template <typename T>
    Array::Array(T* carray, int s):Array(s)
     {
     	for (int i = 0; i < s; i++)
     	array[i] = carray[i];
     }
     
     //copy assignment operator
     
     template <typename T>
     Array& Array::operator=(const Array& rhs)   
     {
     	// check for self-assignment and if so, perform a no-op
     	if (this==&rhs)
     	return *this;
     	cout << "Copying array." << endl;
     	//resize target array if needed
     	if (size != rhs.size)
     	{
     		size=rhs.size;
     		delete[] array;
     		array = new T[size];
     	}
     	//copy data
     	for (int i = 0; i < size; i++)
     		array[i] =rhs.array[i];
     		//done
     		return *this;
     }
     
     template <typename T>
     Array& Array::operator=(Array&& rhs)
     {
     	//not checking for self-assignment as rhs is most likely temporary
     	cout << "Moving array." << endl;
     	
     	// reallocate array size if need be
     	if (size != rhs.size)
     	{
     		size=rhs.size;
     	    delete[] array;
     	    array = new T[size];
     	}
     	//copy data
     	for (int i = 0; i < size; i++)
     	array[i] = rhs.array[i];
     	//invalidate rhs
     	delete[] rhs.array;
     	rhs.array=0;
     	//done
     	return *this;
     }
     
     //destructor
    Array::~Array()
    {
    	cout << "Destroying array." << endl;
    	//deallocate the wrapped array
    	delete[] array;
    	array = 0;
    }
    
    //index operator
    template <typename T>
    T& Array::operator[](int index) const
    {
    	//perform bounds checking
    	if (index < 0 || index >= size)
    	{
    	 cout << "Array index must not be negative or greater then or equal to array size." << endl;
    	 system("ABORT");
    	}
    	else
    	return array[index];
    }
    
    //conversion to regular array
    template <typename T>
    Array::operator T*() const
    {
    	return array;
    }
    
    //getter for size
    int Array::getsize() const
    {
    	return size;
    }
    
    //input validation function
    void IsInputValid(bool condition)
    {
    	if(!condition)
    	throw InvalidInput();
    	else
    	return;
    }
    Any ideas? Debugging templates has always been my weak suit.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Template error help

    Basically, instead of:
    Code:
    //regular constructor
    template <typename T>
    Array::Array(int s): size(s)
    {
    you should have written:
    Code:
    //regular constructor
    template <typename T>
    Array<T>::Array(int s): size(s)
    {
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Dec 2013
    Posts
    75

    Re: Template error help

    Lol, I'm ignorant.

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