What we are to do is pretty much mentioned in the comments of the code.
I have to make a class ARRAY, which searches a list etc...

Right now I can't compile this and I'm so stuck. Please help :)

Array.h

// Put comments about the class here.
 #ifndef ARRAY_H
 #define ARRAY_H
 #include <iostream>
 
 using namespace std;

 
 template <class T>
 class Array
 {
 public:
   static const int NOT_FOUND = -1;
   static const int DEFAULT_SIZE = 100; 
   
	Array(int size = DEFAULT_SIZE); // Constructor
// Creates an array of the size specified by the parameter size. The default array size is 100.

// Postcondition: The list points to the array, length = size

// Copy constructor
Array(const Array<T>& rhs);

~Array(); // Destructor

// Deallocates the memory occupied by the array.

// Overloads the assignment operator
const Array<T>& operator= (const Array<T>&);

// Overloads the equality operator
bool operator== (const Array & rhs) const;

// Overloads the non-equality operator
bool operator!= (const Array & rhs) const;

// Method to determine the size of the array.
int get_size (void) const;

const T& operator[](int index) const;// Method to return an element in the array that will not be changed.

T& operator[](int index); // Method to return an element in the array that can be changed.

// Method to search the list for the first instance of the given item.
int search(const T& itemFound) const;

// Postcondition: If the item is found, returns the location in the array where the item is first found; otherwise, returns -1.

int find_first (const T & element) const;

  // Function to output the elements of the list to the ostream
  // passed in.
  template <typename U>
  friend ostream & operator<< (ostream & os,
                                    const Array<U> & array);

private:

	T * elements_; //pointer to the array elements
	int length_; //the length of the list
	int position_;
	int size_;
};
/* Define the methods in the header file since the compiler
** needs these definitions to create the element-specific array
** class.
*/



// Constructor
 template <class T>
 Array<T>::Array(int size)
 : size_(size), elements_(new T[size])
 {
 		position_ = 0;
 }

// Copy constructor
//template <class T>
//Array<T>(const T& copy)
//: size_(copy.size), elements_(new T[copy.size]) 
//{
//    copy(copy.elements_, copy.elements_+ copy.size, elements_);    // #include <algorithm> for std::copy
//}


// Destructor
 template <class T>
 Array<T>::~Array()
 {
 		 delete []elements_;
 }

// Overloads the assignment operator
template <class T>
const Array<T>&
Array<T>::operator= (const Array<T>& rhs)
{
  if( size_ != rhs.size_ )//also checks if on both side same object
    {
      delete []elements_;
      elements_= new int[rhs.size];
    }
  size_=rhs.size;
  position_=rhs.position_;
  for( int i = 0; i < position_; i++ )
    elements_[i]=rhs.elements_[i];
  return *this;
}


// Overloads the equality operator return!  (*this == another_object)


// Overloads the non-equality operator
//bool Array::operator!=(const Array &array)
//{
//		return !(*this == another_object);
//}

// Method to determine the size of the array.
//const T& Array<T>::sizeOfArray()

// Method to return an element in the array that will not be changed.
 template <class T>
 const T& Array<T>::operator[](int index)const
 {
		 return this->elements_[index];
 }



// Method to return an element in the array that can be changed.
 template <class T>
 T& Array<T>::operator[](int index)
 {
		 return this->elements_[index];
 }

// Method to search the list for the first instance of the given item.
 template <class T>
 int Array<T>::search(const T& itemFound) const
 {
		 bool itemIsFound = false;
		 int location;
		
		 for(location = 0; location < size_; location++)
		 {
				 if(elements_[location] == itemFound)
				 {
						 itemIsFound = true;
				 }
		 }
			
		 if(itemIsFound)
			 return location;
		 else
			 return -1;		
 }

// Function to output the elements of the list to the ostream passed in.
 #endif

test.cpp

#include <assert.h>
#include "Array.h"

using namespace std;

int
main (int argc,
      char *argv[])
{
  // Create and initialize an array of ints
  Array<int> int_array1;

  for (int i = 0; i < Array<int>::DEFAULT_SIZE; ++i)
    {
      int_array1[i] = i;
    }

  // Right now the code below just prints out the array
  // and requires visual checking which is tedious and error-prone.
  // Change testing the output of an array via a script.
  // CHANGE THIS
  std::cout << int_array1 << std::endl;

  const Array<int> int_array2 (int_array1);

  // Check copy constructor
  assert (int_array1 == int_array2);

  // Create another int array to check !=
  Array<int> int_array3;

  for (int i = Array<int>::DEFAULT_SIZE; i > 0; --i)
    {
      int_array3[i] = i - 1;
    }

  // Check !=
  assert (int_array1 != int_array3);

  // Change an element in the first array so it's no longer
  // equal to the second array.
  int_array1[3] = 0;
  assert (int_array1 != int_array2);

  // Test the find_first method.
  assert (int_array1.find_first (10) == 10);

  // Set an earlier element to the value 10 and recheck
  // the find_first method.
  int_array1[4] = 10;
  assert (int_array1.find_first (10) == 4);

  // ADD MORE TESTS TO CHECK *ALL* THE METHODS ON THE ARRAY CLASS.

  return 0;
}

At line 103, you should have elements_= new T[rhs.size]; (notice "int" changed to "T").

In you copy-constructor, it is not a good idea to name the parameter copy because there will be a name conflict with std::copy that is used in the same function. And you should qualify the call to std::copy in full, not just copy .

At line 152, you need to break out of the loop. So, insert a break; statement just after line 152.

In your assignment operator, you should check for self-assignment with a simple if( this == &rhs ) return *this; statement at the beginning.

You write std::cout << int_array1 << std::endl; in your test program, but you haven't created an overloaded function for the << operator and your Array class template. So, you should add, within you class template declaration, something along the lines of:

friend
  std::ostream& operator <<(std::ostream& out, const Array<T>& rhs) {
    for(int i = 0; i < rhs.size_; ++i)
      out << rhs.elements_[i] << std::endl;
    return out;
  };

In the future, if your question is about compilation errors, you should provide the error messages and mark the specific lines to which your compiler points you as being the faulty ones.

At line 103, you should have elements_= new T[rhs.size]; (notice "int" changed to "T").

In you copy-constructor, it is not a good idea to name the parameter copy because there will be a name conflict with std::copy that is used in the same function. And you should qualify the call to std::copy in full, not just copy .

At line 152, you need to break out of the loop. So, insert a break; statement just after line 152.

In your assignment operator, you should check for self-assignment with a simple if( this == &rhs ) return *this; statement at the beginning.

You write std::cout << int_array1 << std::endl; in your test program, but you haven't created an overloaded function for the << operator and your Array class template. So, you should add, within you class template declaration, something along the lines of:

friend
  std::ostream& operator <<(std::ostream& out, const Array<T>& rhs) {
    for(int i = 0; i < rhs.size_; ++i)
      out << rhs.elements_[i] << std::endl;
    return out;
  };

In the future, if your question is about compilation errors, you should provide the error messages and mark the specific lines to which your compiler points you as being the faulty ones.

Thanks a ton! Helped a lot :)

At line 103, you should have elements_= new T[rhs.size]; (notice "int" changed to "T").

In you copy-constructor, it is not a good idea to name the parameter copy because there will be a name conflict with std::copy that is used in the same function. And you should qualify the call to std::copy in full, not just copy .

At line 152, you need to break out of the loop. So, insert a break; statement just after line 152.

In your assignment operator, you should check for self-assignment with a simple if( this == &rhs ) return *this; statement at the beginning.

You write std::cout << int_array1 << std::endl; in your test program, but you haven't created an overloaded function for the << operator and your Array class template. So, you should add, within you class template declaration, something along the lines of:

friend
  std::ostream& operator <<(std::ostream& out, const Array<T>& rhs) {
    for(int i = 0; i < rhs.size_; ++i)
      out << rhs.elements_[i] << std::endl;
    return out;
  };

In the future, if your question is about compilation errors, you should provide the error messages and mark the specific lines to which your compiler points you as being the faulty ones.

I'm confused with the comment about adding a "break".

DO you think you could expand on that because when I add a break to line 153 there is an error. Thanks.

You're loop is a basic linear search. You go through each element to find a match for the given value. When you do find a match (line 150), you set a bool to true to signify that the match was found. However, you don't stop the loop. It will continue to loop around till the end of the array, which is not only useless but wrong. At the end of the loop, you check the bool and if you found a match you return the location of that match, but since you always iterate to the end of the array, that returned location will always be at the end of the array, that's wrong. At the very least, you need to stop the loop, and break is the C/C++ keyword to do that.

An even better way to do it is simply like this:

// Method to search the list for the first instance of the given item.
 template <class T>
 int Array<T>::search(const T& itemFound) const
 {
		 for(int location = 0; location < size_; location++)
		 {
				 if(elements_[location] == itemFound)
				 {
						 return location;
				 }
		 }
		 return -1;
 }
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.