I 부울 연산자 다음과 같은 문제를 가지고()부울 연산자()와 파생 클래스의 상속
기본 클래스
class Point
{
double x, y;
public:
Point(){x=0;y=0;}
...
}
파생 클래스
class 3DPoint : public Point
{
double z;
public:
3DPoint(double x, double y, double zx) : Point(x,y){z(zz);}
...
}
연산자()
class compareByX
{
bool operator() (const 3DPoint *p1, const 3DPoint *p2) const
{
return p1->x < p2->x; //Compilation error
}
}
c 점의 작성자
class List: public list<3DPoint *>
{
...
}
int main()
{
List l;;
l.push_back(new 3DPoint(1,2,3));
l.push_back(new 3DPoint(4,5,6));
sort(l.begin(), l.end(), compareByX);
}
컴파일은 compareByX 클래스에서 다음 메시지와 함께 중단됩니다. 3DPoint const를 Point로 변환 할 수 없습니다. const 선언을 삭제했습니다 ...
class compareByX
{
bool operator() (3DPoint *p1, 3DPoint *p2) const
{
return p1->x < p2->x; //Compilation error
}
}
... 및 ... 성공적인 컴파일. 하지만 operator()는 잘 정의되어 있지 않습니다. 도와 줄수있으세요? 아마도 더 적합한 객체 모델을 제안하는 것이 낫습니다 ... 고맙습니다.
3DPoint? 식별자는 숫자로 시작할 수 없습니다. –
죄송하지만 C#이 아닙니다. C++과 비슷한가? –
@ Michael Stum : 예, 그렇습니다. 그리고 태그를 고쳤습니다. – dirkgently