【发布时间】:2020-07-02 02:01:41
【问题描述】:
在Line.h 中,我有Line 类和一个名为length() 的公共函数,用于测量向量中所有点之间的距离(特别是points[10])。
在Point.h 中,我有一个名为distance() 的函数,用于测量图形上两点之间的距离。
有没有办法让我使用length() 内部的distance() 函数来测量每个点之间的距离之和?
由于我还没有完成此作业的所有代码,因此存在一些问题,这只是我目前遇到的问题。
//Line.cpp
#include "Point.h"
#include "FloatCompare.h"
#include "Line.h"
#include <stdexcept>
//default constructor
Line::Line(){
this->index = 0;
}
//destructor
Line::~Line(){
}
/**
* Add a point to the end of our line. If the line contains
* ten points then throw an out_of_range exception.
*/
/**
*
if index == 10 then
throw out-of-range exception
else
point[index] = p argument
index++ **/
void push_back(const Point& p){
unsigned int index =0;
Point points[10];
if (index == 10){
throw std::out_of_range ("Out of range.");}
else {
points[index] = p;
index++;}
}
/**
* Clear the list of points
*/
void clear(){
unsigned int index = 0;
}
/**
* Return the length of the line. The length is calculated as
* the sum of the distance between all points in the line.
*/
//this is where I am having my issues.
double length(){
float total = 0.0;
Point points[10];
for (unsigned int index = 0;index < 10; index++){
total += Point distance(points &index);
}
return total;
}
Line.h 包含Line.cpp 的构造函数
/*
* Line.h
*
*/
#ifndef LINE_H_
#define LINE_H_
#include "Point.h"
class Line {
public:
/**
* Constructor and destructor
*/
Line();
virtual ~Line();
/**
* Add a point to the end of our line. If the line contains
* ten points then throw an out_of_range exception.
*/
void push_back(const Point& p);
/**
* Clear the list of points
*/
void clear();
/**
* Return the length of the line. The length is calculated as
* the sum of the distance between all points in the line.
*/
double length();
private:
unsigned int index;
Point points[10];
};
#endif /* LINE_H_ */
我感觉我可能做错了,我可能不需要调用distance() 函数。如果我需要包含任何其他代码、.h 或 .cpp 文件,请告诉我。
编辑 - 这是 Point.h 和 Point.cpp 文件
点.h
#ifndef POINT_H_
#define POINT_H_
#include <iostream>
class Point {
public:
/**
* Constructor and destructor
*/
Point();
Point(const double x, const double y);
virtual ~Point();
/**
* Get the x value
*/
double getX() const;
/**
* Get the y value
*/
double getY() const;
/**
* Return the distance between Points
*/
double distance(const Point &p) const;
/**
* Output the Point as (x, y) to an output stream
*/
friend std::ostream& operator <<(std::ostream &out, const Point &point);
/**
* Declare comparison relationships
*/
friend bool operator ==(const Point &lhs, const Point &rhs);
friend bool operator <(const Point &lhs, const Point &rhs);
/**
* Declare math operators
*/
friend Point operator +(const Point &lhs, const Point &rhs);
friend Point operator -(const Point &lhs, const Point &rhs);
/**
* Copy constructor
*/
Point(const Point& t);
/**
* Copy assignment
*/
Point &operator =( const Point& rhs );
private:
double x;
double y;
};
#endif /* POINT_H_ */
Point.cpp
#include "Point.h"
#include "FloatCompare.h"
#include "Line.h"
#include <cmath>
/** default constructor **/
Point::Point(){
// TODO Auto-generated constructor stub
this->x = 0.0;
this->y = 0.0;
}
//Overloaded Constructor
Point::Point(const double x, const double y){
// TODO Auto-generated constructor stub
this->x = x;
this->y = y;
}
/**
* Copy constructor */
Point::Point(const Point & t){
x = t.x;
y = t.y;
}
//Default destructor
Point::~Point() {
// TODO Auto-generated destructor stub
}
//Retrieve X value
double Point::getX() const {
return x;
}
//Retrieve Y value
double Point::getY() const {
return y;
}
//Function for finding distance between 2 points on a graph. Using ((x-p.x)^2+(y-p.y)^2)^1/2
double Point::distance(const Point &p) const{
return sqrt( pow((x - p.getX()),2) + pow((y - p.getY()),2));
}
/**
* Output the Point as (x, y) to an output stream
*/
std::ostream& operator <<(std::ostream &out , const Point &point){
out <<'(' << point.x << ',' << ' ' << point.y << ')';
return out;
}
/**
* Declare comparison relationships
*/
//Equal to
bool operator ==(const Point &lhs, const Point &rhs){
return essentiallyEqual(lhs.getX(), rhs.getX()) && essentiallyEqual(lhs.getY(), rhs.getY());
}
//Comparison less than
bool operator <(const Point &lhs, const Point &rhs){
//P1 < P2 if P1.x < P2.x || (P1.x == P2.x && P1.y < P2.y).
return definitelyLessThan(lhs.getX(), rhs.getX()) || (essentiallyEqual(lhs.getX(), rhs.getX()) && definitelyLessThan(lhs.getY(), rhs.getY()));
}
/**
* Declare math operators
*/
//Addition Operator
Point operator +(const Point &lhs, const Point &rhs){
return Point(lhs.x + rhs.x, lhs.y + rhs.y);
}
//Subtraction Operator
Point operator -(const Point &lhs, const Point &rhs){
return Point(lhs.x - rhs.x, lhs.y - rhs.y);
}
/**
* Copy assignment
*/
Point & Point::operator =(const Point& rhs){
x= rhs.x;
y= rhs.y;
return *this;
}
【问题讨论】:
-
您的
Line.cpp定义缺少必需的Line::限定符,即将void push_back(const Point& p) {...}更改为void Line::push_back(const Point& p) {...},clear()和length()也是如此。但更重要的是,你没有显示Point.h,所以我们看不到distance()实际上是如何声明的,但Point distance(points &index);无论如何声明都绝对是错误的语法。 -
我添加了 Point.h 和 Point.cpp。希望这会有所帮助,还有两个文件 Floatcompare.cpp 和 .h。但在这一点上,我认为他们没有必要发布。
标签: c++ class vector member graphing