簡體   English   中英

C ++中與運算符有關的模板錯誤

[英]Errors with a Template in C++ regarding operators

在我的項目中,我將頭文件用於arrayList。 在main方法期間,我初始化了arrayList類型的對象,其中FriendToken是我的項目中定義的另一個類。 但是,這在編譯arrayList.h時給了我很多錯誤。 顯然,我無法使用內置的復制方法,並且FriendToken類型的對象無法識別運算符==。 我是否應該為FriendToken重載==運算符,如果是,應該怎么做?

這兩個錯誤都在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());
}

沒有編譯器錯誤很難分辨。

無論哪種方式,這都是使操作員超載的方式。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM