【问题标题】:Is it possible to implement a function from another class into a separate class function是否可以将另一个类的函数实现为单独的类函数
【发布时间】: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&amp; p) {...} 更改为void Line::push_back(const Point&amp; p) {...}clear()length() 也是如此。但更重要的是,你没有显示Point.h,所以我们看不到distance()实际上是如何声明的,但Point distance(points &amp;index);无论如何声明都绝对是错误的语法。
  • 我添加了 Point.h 和 Point.cpp。希望这会有所帮助,还有两个文件 Floatcompare.cpp 和 .h。但在这一点上,我认为他们没有必要发布。

标签: c++ class vector member graphing


【解决方案1】:

几乎您的整个Line.cpp 文件编码不正确。

push_back()clear()length() 的定义缺少必需的 Line:: 限定符。

push_back()length() 中有一个名为points[] 的局部变量,在push_back()clear() 中有一个名为index 的局部变量,它们隐藏了同名的类成员。你需要摆脱那些局部变量。

但是,最重要的是,您在length() 内部调用distance() 的语法完全错误。 distance()Point 的一个非静态方法,并以单个 Point 作为输入。这意味着您需要从points[] 数组中获取一个Point 对象并在其上调用distance(),然后将同一数组中的下一个Point 对象传递给它。

另外,length() 内部的循环迭代次数过多。例如,如果只将 5 个点推入一条线,则您将遍历数组的所有 10 个元素。

试试这个:

//Line.cpp
#include "Point.h"
#include "FloatCompare.h"
#include "Line.h"
#include <stdexcept>

//default constructor
Line::Line(){
    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 Line::push_back(const Point& p){
    if (index == 10){
        throw std::out_of_range ("Out of range.");
    }
    points[index] = p;
    index++;
}

/**
 * Clear the list of points
 */
void Line::clear(){
    index = 0;
}

/**
 * Return the length of the line. The length is calculated as
 * the sum of the distance between all points in the line.
 */
double Line::length(){
    double total = 0.0;
    for (unsigned int i = 1; i < index; i++){
        total += points[i-1].distance(points[i]);
    }
    return total;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 2016-09-23
    • 1970-01-01
    • 2012-03-07
    相关资源
    最近更新 更多