0

In my project, I am using a header file for an arrayList. During the main method, I initialize an object of type arrayList where FriendToken is another class defined in my project. However, this gives me quite a few errors while compiling arrayList.h. Apparently, I cannot use the built-in copy method and the operator == is unrecognized for an object of type FriendToken. Should I overload the == operator for FriendToken and if so, how should I do that?

Both errors are marked in the body ArrayList.h.

ArrayList.h:

#ifndef arrayList_h
#define arrayList_h

#include "linearList.h"

#include <iostream>
#include <fstream>
#include <ostream>

using namespace std;
template<class T>
class arrayList : public linearList<T> 
{
public:
    // constructor, copy constructor and destructor
    arrayList(int initialCapacity = 10);
    arrayList(const arrayList<T>&);
    ~arrayList() {delete [] element;}
    // ADT methods
    bool empty() const {return listSize == 0;}
    int size() const {return listSize;}
    T& get(int theIndex) const;
    int indexOf(const T& theElement) const;
    void erase(int theIndex);
    void insert(int theIndex, const T& theElement);
    void output(ostream& out) const;
    void changeLength1D(T*& a, int oldLength, int newLength);
    // additional method
    int capacity() const {return arrayLength;}
protected:
    void checkIndex(int theIndex) const;
    // throw illegalIndex if theIndex invalid
    T* element;      // 1D array to hold list elements
    int arrayLength;       // capacity of the 1D array
    int listSize;          // number of elements in list
};
template<class T> 
arrayList<T>::arrayList(int initialCapacity)
{
    // Constructor.
    arrayLength = initialCapacity;
    element = new T[arrayLength];
    listSize = 0;
}
template<class T>
arrayList<T>::arrayList(const arrayList<T>& theList)
{
    // Copy constructor.
    arrayLength = theList.arrayLength;
    listSize = theList.listSize;
    element = new T[arrayLength];
    copy(theList.element, theList.element + listSize, element); 
}
template<class T>
void arrayList<T>::checkIndex(int theIndex) const
{
    // Verify that theIndex is between 0 and 
    // listSize - 1.
    if (theIndex < 0 || theIndex >= listSize)
    {
        cout << "index = " << theIndex << " size = " 
            << listSize;
    } 
}
template<class T>
T& arrayList<T>::get(int theIndex) const
{
    // Return element whose index is theIndex.
    // Throw illegalIndex exception if no such
    // element.
    checkIndex(theIndex);
    return element[theIndex];
}
template<class T>
int arrayList<T>::indexOf(const T& theElement)const
{
    // Return index of first occurrence of theElement.
        // search for theElement
        int theIndex = (int) (find(element, element
        + listSize, theElement) - element);
    // check if theElement was found
    if (theIndex == listSize)
        return -1; // not found
    else return theIndex; 
}
template<class T>
void arrayList<T>::erase(int theIndex)
    {// Delete the element whose index is theIndex.
    checkIndex(theIndex);
    // valid index, shift elements with higher index
//PROBLEM********************************************
    copy(element + theIndex + 1, element + listSize,element + theIndex);
    element[--listSize].~T(); // invoke destructor
}
template<class T>
void arrayList<T>::insert(int theIndex, const T& theElement)
{
    // Insert theElement.
    if (theIndex < 0 || theIndex > listSize)

    {// invalid index
        // code to throw an exception comes here
    }
    // valid index, make sure we have space
    if (listSize == arrayLength)
    {
        // no space, double capacity
        changeLength1D(element, arrayLength, 
        2 * arrayLength);
        arrayLength *= 2;
    }
    // shift elements right one position
//PROBLEM***************************************
    copy_backward(element + theIndex, element + listSize, element + listSize + 1);
    element[theIndex] = theElement;
    listSize++;
}
template<class T>
void arrayList<T>::output(ostream& out) const
{
    // Put the list into the stream out.
    copy(element, element + listSize, ostream_iterator<T>(out, "  ")); 
}
template <class T>
ostream& operator<<(ostream& out, const arrayList<T>& x)
{x.output(out); return out;}

template<class T>
void changeLength1D(T*& a, int oldLength, int newLength)
{
    if (newLength < 0)
        throw illegalParameterValue();
    T* temp = new T[newLength];     
    // new array
    int number = min(oldLength, newLength); 
    // number to copy
    copy(a, a + number, temp);
    delete [] a;                    
    // deallocate old memory
    a = temp;
}


#endif

FriendToken.h

#ifndef FriendToken_h
#define FriendToken_h

#include <string>

using namespace std; 

class FriendToken
{
private:
    string birthDate, name, homeTown;
public:
    FriendToken(string birthDate = "01/01", string name = "John, Smith", string homeTown = "New York");
    string getBirthDate();
    string getName();
    string getHomeTown();
    bool equals(FriendToken a);
};

#endif

FriendToken.cpp

#include "FriendToken.h"
#include <string>

using namespace std;

FriendToken::FriendToken(string birthDate, string name, string homeTown)
{
    this->birthDate = birthDate;
    this->name = name;
    this->homeTown = homeTown;
}
string FriendToken::getBirthDate()
{
    return birthDate;
}
string FriendToken:: getName()
{
    return name;
}
string FriendToken::getHomeTown()
{
    return homeTown;
}
bool FriendToken::equals(FriendToken a)
{
    return (name == a.getName()) && (homeTown == a.getHomeTown()) && (birthDate == a.getBirthDate());
}
4

1 回答 1

0

It's hard to tell without the compiler errors.

Either way, this is how you overload the operator.

template<typename T>
bool arrayList::operator== (const arrayList<T>& theList) 
{
    // Compare the values, and return a bool result.
}
于 2013-02-18T14:59:10.813 回答