C++ 特定于对象的数组列表C++;问题
我目前有一个Person类,并创建了一个PersonList类来扩展List,特别是针对Person类型的对象。当我实例化一个新的PersonList时,我得到一个错误,它阻止了整个构建的成功进行: 错误C2678:二进制“==”:未找到接受“Person”类型的左手操作数的运算符(或没有可接受的转换)c:\program files\microsoft visual studio 11.0\vc\include\xutility 3186 1 Console应用程序3 以下是PersonList类:C++ 特定于对象的数组列表C++;问题,c++,C++,我目前有一个Person类,并创建了一个PersonList类来扩展List,特别是针对Person类型的对象。当我实例化一个新的PersonList时,我得到一个错误,它阻止了整个构建的成功进行: 错误C2678:二进制“==”:未找到接受“Person”类型的左手操作数的运算符(或没有可接受的转换)c:\program files\microsoft visual studio 11.0\vc\include\xutility 3186 1 Console应用程序3 以下是PersonLis
#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一次
#包括
#包括
#包括
#包括
#包括“linearList.h”
#包括“myExceptions.h”
#包括“变更长度1d.h”
#包括
使用名称空间std;
模板
类个人列表:公共直线列表
{
公众:
个人列表(int initialCapacity=10);
PersonList(const PersonList&);
~PersonList(){delete[]元素;}
bool empty()常量{return listSize==0;}
int size()常量{return listSize;}
人员和获取(内部索引)常量;
int indexOf(const Person&theElement)const;
无效擦除(int-theIndex);
无效插入(输入索引、const Person和theElement);
无效输出(ostream&out)常数;
//附加方法
int capacity()常量{return arraylelength;}
受保护的:
无效检查索引(int theIndex)常量;
//如果索引无效,则抛出非法索引
Person*element;//1D数组用于保存列表元素
int-arrayLength;//1D数组的容量
int listSize;//列表中的元素数
};
模板
PersonList::PersonList(int initialCapacity)
{//构造函数。
如果(初始容量<1)
{奥斯汀河s;
函数indexOf()
包含对STL的std::find()
算法的调用,该算法在内部执行值对之间的比较,以确定作为第三个参数传递的元素是否包含在前两个参数定义的范围内
要执行此比较,std::find()
使用=
运算符。但是,没有为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) && ...
}
};
operator==
是为Person
类实现的吗?否,如何准确实现运算符?linearList是一个抽象类,定义列表中使用的主要方法structures@AlexAlex,只需比较那些需要比较的成员。@AlexAlex,好的,这似乎适用于“==”。然后它会显示“@AlexAlex:是的。这是因为您使用了std::copy
和ostream\u迭代器
,这实际上会导致输出插入。输出插入使用运算符
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) && ...
}
};