2013-02-18 1 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

+0

클래스에'operator =='함수를 추가해 보았습니까? 도움이 되었습니까? –

+0

다소 도움이됩니다. 운영자 오류는 더 이상 발생하지 않지만 copyThread 및 Copy_backward 메서드는 여전히 FriendToken에서 작동하지 않는다고 말합니다. –

답변

0

이 컴파일러 오류없이 말할 어렵다.

어느 쪽이든, 이것은 당신이 연산자를 오버로드하는 방법입니다.

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