operator==()运算符重载为什么不能声明为友元函数?
#pragma once #ifndef COORDINATE_H #define COORDINATE_H #include <iostream> using namespace std; class Coordinate{ public: friend bool operator==(Coordinate &, Coordinate &); friend ostream& operator<<(ostream &os, Coordinate &coor); Coordinate& operator=(const Coordinate &rhs); Coordinate(int x=0, int y=0); Coordinate(const Coordinate &rhs); //void printCoordinate(); protected: private: int m_iX; int m_iY; }; Coordinate::Coordinate(int x, int y) { m_iX = x; m_iY = y; } Coordinate::Coordinate(const Coordinate &rhs) { m_iX = rhs.m_iX; m_iY = rhs.m_iY; } Coordinate & Coordinate::operator=(const Coordinate &rhs) { m_iX = rhs.m_iX; m_iY = rhs.m_iY; return *this; } ostream& operator<<(ostream &os, Coordinate &coor) { os << "(" << coor.m_iX << "," << coor.m_iY << ")" << endl; return os; } bool operator==(Coordinate &lhs,Coordinate &rhs) { if (lhs.m_iX==rhs.m_iX&&lhs.m_iY==rhs.m_iY) return true; else return false; } #endif // !COORDINATE_H
operator==运算符重载仍然在下面这个成员函数中报错
int MyList::LocateMyList(Coordinate * p) { for (int i = 0; i < m_iLength;i++) { if (m_pList[i] == *p) //error:没有与这些操作数匹配的"=="运算符 return i; } return -1; }