我有一个Line类UML图,其中显示了以下详细信息
- pOne: Point
- pTwo: Point
+ Line (ptOne: Point, ptTwo: Point)
+ getPOne () : Point
+ getPTwo () : Point
+ setPOne (pOne: Point)
+ setPTwo (pTwo: Point)
)
根据我对UML图的解释
,这就是我所做的Line.h
#ifndef __testing__Line__
#define __testing__Line__
#include <iostream>
#include "Point"
class Line {
private:
//pOne and pTwo are objects of Point
Point pOne;
Point pTwo;
public:
Line() {
};//default Constructor
// constructor of class Line to store objects of Point(pOne,pTwo)
Line (Point pOne,Point pTwo);
// get method for objects of Point(pOne,pTwo)
Point getPOne();
Point getPTwo();
// set method for objects of a(one,two)
void setPOne (Point pOne);
void setPTwo (Point pTwo);
};
#endif /* defined(__testing__Line__) */
Line.cpp
#include "Line.h"
Line::Line (Point pOne, Point pTwo) {
setPOne(pOne);
setPTwo(pTwo);
}
Point Line::getPOne() {
return pOne;
}
Point Line::getPTwo() {
return pTwo;
}
Line::setPOne (Point pOne) {
this-> pOne = pOne;
}
Line::setATwo (Point pTwo) {
this->pTwo = pTwo;
}
在main cpp中我尝试调用函数getPOne()
的main.cpp
#include "Point"
#include "Line"
int main() {
Line outputMethod;
//Invalid operands to binary expression
std::cout << outputMethod.getPOne() << std::endl;
}
如何在上述情况下从Line类调用getPOne()?
运算符重载
//overload << operator
friend std::ostream& operator<<(std::ostream& os, Point&)
{
return os;
}
答案 0 :(得分:0)
您的实施有两个问题:
您不应该从Line
派生Point
课程 - 语义错误(Line
不是Point
)并且根据UML描述也是如此。
您需要为<<
类定义专有运算符Point
,才能将其与cout
一起使用。如果你不这样做,编译器就不知道如何打印你的数据(毕竟这是你专有的定义数据)。