2013-02-18 113 views
0

在我的項目中,我正在使用arrayList的頭文件。在main方法期間,我初始化一個arrayList類型的對象,其中FriendToken是我的項目中定義的另一個類。但是,這在編譯arrayList.h時給了我很多錯誤。顯然,我不能使用內置的複製方法,並且對於FriendToken類型的對象,運算符==無法識別。我應該爲FriendToken重載==運算符嗎?如果是的話,我該怎麼做?關於運算符的C++中的模板錯誤

這兩個錯誤都標記在主體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()); 
} 
+1

這是大量的代碼來閱讀。也許你可以縮小範圍。 – LihO 2013-02-18 11:04:13

+0

您是否曾嘗試在班級中添加'operator =='函數?它有幫助嗎? – 2013-02-18 11:06:15

+0

這有些幫助。操作員錯誤不再發生,但方法copy和copy_backward仍然表示它們不適用於FriendToken。 – 2013-02-18 11:30:02

回答

0

很難說沒有編譯器錯誤。

無論哪種方式,這是如何超載運營商。

template<typename T> 
bool arrayList::operator== (const arrayList<T>& theList) 
{ 
    // Compare the values, and return a bool result. 
}