My methods dont seem to be working for example void Resize doeznt do anything. AM i using it in the wrong way oris the method faulty.

//Header file for Array.cpp.
#ifndef ARRAY_H
#define ARRAY_H
template<class Datatype>
class Array
{

private:
	//Template for use with the Array's data type.
	//Member Variables.
	int size;
	int grow_size;
	int num_elements;


public:
	//Constructor.
	Array( int p_size)
	{
		m_array = new Datatype[p_size];
		size = p_size;
	}




	//Destructor.
	~Array()
	{
		if( m_array!= 0 )
			delete[] m_array;
		m_array= 0;
	}

	//Access Operator: Syntax to allow the use of editing.
	Datatype& operator[] ( int p_index)
	{
		return m_array[p_index];
	}


	//Functions.
	int m_size;
	Datatype* m_array;
	void ArrayFunction( int* p_array)
	{
	p_array[0] = 10;
	}
	//Print Function: To print out all elemenst in the Array.
	void printArray()
	{
		for(int i=0;i< size;i++)
		{
			cout << "Position: " <<m_array[i]<<endl;
		}


	}

	//Resize Function: To resize the Array.
	void Resize( int p_size)
	{
		Datatype* newarray = new Datatype[p_size];
		if( newarray== 0 )
			return;
		int min;
		if( p_size< m_size)
			min = p_size;
		else
			min = m_size;
		int index;
		for( index = 0; index < min; index++ )
			newarray[index] = m_array[index];
		m_size= p_size;
		if( m_array!= 0 )
			delete[] m_array;
		m_array= newarray;
	}

	//Remove Function: To remove an element in an Array.
	void Remove( int p_index)
	{
		int index;
		for( index = p_index+ 1; index < m_size; index++ )
			m_array[index -1] = m_array[index];
	}
	//Size Function: To get the size of the Array.
	int Size()
	{
		return m_size;
	}
	//Insert Function: To insert an element into the array between points in the index.
	void Insert( Datatype p_item, int p_index)
	{
		int index;
		for( index = m_size-1; index > p_index; index—)
			m_array[index] = m_array[index -1];
		m_array[p_index] = p_item;
	}



};

#endif
#include"Array.h"//To allow the use of Array.h Header file.
#include<iostream>
using namespace std;// To allow the use of the standard library.

int main()//Main method for CA1Arrays.
{
	//Creating an array.
	Array<int> intArray(10);
	//Filling the Array(intArray)with elements from 1-10.
	intArray[0] = 1;
	intArray[1] = 200;
	intArray[2] = 323;
	intArray[3] = 976;
	intArray[4] = 545;
	intArray[5] = 767;
	intArray[6] = 234;
	intArray[7] = 123;
	intArray[8] = 456;
	intArray[9] = 789;
	
	intArray.printArray();
	intArray.Remove(7);
	intArray.printArray();
	intArray.Size();


getchar();
getchar();
}

Recommended Answers

All 3 Replies

Your problem is in your array class, you have declared half you data members at the top of the class (line 9) and then some more half way down (line 42) and as a result you have 2 size members which you use interchangeably but you only initialize one of them.

1. Put all of the class data members in the same place
2. Remove 1 of the 2 size members

Your problem is in your array class, you have declared half you data members at the top of the class (line 9) and then some more half way down (line 42) and as a result you have 2 size members which you use interchangeably but you only initialize one of them.

1. Put all of the class data members in the same place
2. Remove 1 of the 2 size members

Anytime i remove one of the member variables i get this error:
c:\users\conor\documents\visual studio 2010\projects\ca1arrays\ca1arrays\array.cpp(8) : see reference to class template instantiation 'Array<Datatype>' being compiled
1> with
1> [
1> Datatype=int

Anytime i remove one of the member variables i get this error:
c:\users\conor\documents\visual studio 2010\projects\ca1arrays\ca1arrays\array.cpp(8) : see reference to class template instantiation 'Array<Datatype>' being compiled
1> with
1> [
1> Datatype=int

I actually got it workin while usins size as the member variable and replacing m_size but the functions appear to be still not working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.