0

我目前有一个 Person 类,并创建了一个扩展 List 的 PersonList 类,专门用于 Person 类型的对象。当我实例化一个新的 PersonList 时,我收到一个错误,导致整个构建无法成功发生:

错误 C2678:二进制“==”:未找到采用“Person”类型左侧操作数的运算符(或没有可接受的转换)c:\program files\microsoft visual studio 11.0\vc\include\xutility 3186 1控制台应用程序3

这是 PersonList 类:

#pragma once
#include<iostream>
#include<sstream>
#include<string>
#include<algorithm>
#include "linearList.h"
#include "myExceptions.h"
#include "changeLength1D.h"
#include <Person.h>

using namespace std;

template<class Person>
class PersonList: public linearList<Person> 
{
public:
PersonList(int initialCapacity = 10);
PersonList(const PersonList<Person>&);
~PersonList() {delete [] element;}

bool empty() const {return listSize == 0;}
  int size() const {return listSize;}
  Person& get(int theIndex) const;
  int indexOf(const Person& theElement) const;
  void erase(int theIndex);
  void insert(int theIndex, const Person& theElement);
  void output(ostream& out) const;

  // additional method
  int capacity() const {return arrayLength;}

  protected:
  void checkIndex(int theIndex) const;
        // throw illegalIndex if theIndex invalid
  Person* element;            // 1D array to hold list elements
  int arrayLength;       // capacity of the 1D array
  int listSize;          // number of elements in list
  };

template<class Person>
PersonList<Person>::PersonList(int initialCapacity)
{// Constructor.
if (initialCapacity < 1)
{ostringstream s;
  s << "Initial capacity = " << initialCapacity << " Must be > 0";
 throw illegalParameterValue(s.str());
}
arrayLength = initialCapacity;
element = new Person[arrayLength];
listSize = 0;
 }

template<class Person>
PersonList<Person>::PersonList(const PersonList<Person>& theList)
{// Copy constructor.
arrayLength = theList.arrayLength;
listSize = theList.listSize;
element = new Person[arrayLength];
copy(theList.element, theList.element + listSize, element);
}


template<class Person>
void PersonList<Person>::checkIndex(int theIndex) const
{// Verify that theIndex is between 0 and listSize - 1.
if (theIndex < 0 || theIndex >= listSize)
{ostringstream s;
s << "index = " << theIndex << " size = " << listSize;
throw illegalIndex(s.str());
}

}

template<class Person>
Person& PersonList<Person>::get(int theIndex) const
{// Return element whose index is theIndex.
 // Throw illegalIndex exception if no such element.
 checkIndex(theIndex);
 return element[theIndex];
 }   

 template<class Person>
int PersonList<Person>::indexOf(const Person& theElement) const
{// Return index of first occurrence of theElement.
 //  Return -1 if theElement not in list.

  // search for theElement
  int theIndex = (int) (find(element, element + listSize, theElement)
                     - element);

   // check if theElement was found
  if (theIndex == listSize)
   // not found
    return -1;
    else return theIndex;
    }

template<class Person>
void PersonList<Person>::erase(int theIndex)
{// Delete the element whose index is theIndex.
 // Throw illegalIndex exception if no such element.
 checkIndex(theIndex);

 // valid index, shift elements with higher index
 copy(element + theIndex + 1, element + listSize,
                            element + theIndex);

   element[--listSize].~Person(); // invoke destructor
  }



template<class Person>
void PersonList<Person>::insert(int theIndex, const Person& theElement)
{// Insert theElement so that its index is theIndex.
if (theIndex < 0 || theIndex > listSize)
{// invalid index
  ostringstream s;
  s << "index = " << theIndex << " size = " << listSize;
  throw illegalIndex(s.str());
}

// 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
   copy_backward(element + theIndex, element + listSize,
             element + listSize + 1);

    element[theIndex] = theElement;

     listSize++;
    }

template<class Person>
void PersonList<Person>::output(ostream& out) const
{// Put the list into the stream out.
 copy(element, element + listSize, ostream_iterator<Person>(cout, "  "));
 }

// overload <<
template <class Person>
ostream& operator<<(ostream& out, const PersonList<Person>& x)
 {x.output(out); return out;}

人物类:

#pragma once
#include <string>

using namespace std;


class Person
{
public:
Person(string firstName, string lastName, string birthday, string hometown);
Person(void);
~Person(void);
string name;
string birthday;
string hometown;
};

同样的事情发生在我之前尝试使用的 arrayList 类中。有没有办法得到它,所以我可以简单地将人员对象存储在 ArrayList 类型结构中?

4

1 回答 1

1

您的函数indexOf()包含对 STLstd::find()算法的调用,该算法在内部执行值对之间的比较,以确定作为第三个参数传递的元素是否包含在前两个参数定义的范围内。

要执行此比较,std::find()请使用==运算符。但是,没有operator ==为 类型的对象定义Person

为了解决这个问题,您必须==为 的实例重载比较运算符Person。例如,您可以这样做:

class Person
{
    ...
public:
    friend bool operator == (Person const& p1, Person const& p2)
    {
        // Perform the comparison and return "true" if the objects are equal
        return (p1.name == p2.name) && ... 
    }
};
于 2013-02-01T19:25:47.583 回答