2017-02-07 5 views
0

私のプログラムでは、2つの値を持つPointというコンストラクタを作成しました。私はまた、set,get,scaleおよびtranslateの機能を持っています。私はオブジェクトと別のポイントの間の距離を取得するための関数を作成しようとしています。どんな助けも素晴らしいだろうが、私はそれに問題がある。C++ 2点間の距離を取得する関数を作成する

#ifndef POINTMODEL 
#define POINTMODEL 
#define POINTDEB UG 

#include <iostream> 
#include <string.h> 

using namespace std; 

class Point { 
public: 
    Point(void); 
    Point(double anX, double aY); 
    ~Point(); 

    void setPoint(double anX, double aY); 

    double getX(); 
    double getY(); 

    double scaleX(double theX); 
    double scaleY(double theY); 
    void translate(double theX, double theY); 

    void distance(const Point& aPoint); 

protected: 
private: 
    double theX; 
    double theY; 
}; 

inline Point::Point(void) 
{ 
    theX = 1; 
    theY = 1; 
    cout << "\n The default constructor was called" << endl; 
} 

inline Point::Point(double anX, double aY) 
{ 
    cout << "\n regular constructor called"; 
} 

inline Point::~Point() 
{ 
    cout << "\n the destructor was called" << endl; 
} 

inline void Point::setPoint(double anX, double aY) 
{ 
    theX = anX; 
    theY = aY; 
} 

inline double Point::getX() 
{ 
    return theX; 
} 

inline double Point::getY() 
{ 
    return theY; 
} 

inline double Point::scaleX(double theX) 
{ 
    return theX; 
} 

inline double Point::scaleY(double theY) 
{ 
    return theY; 
} 

inline void Point::translate(double offSetX, double offSetY) 
{ 
    cout << "X is translated by : " << offSetX << endl; 
    cout << "Y is translated by : " << offSetY << endl; 
} 

inline void Point::distance(const Point& aPoint) 
{ 
} 

#endif 

CPPファイル:あなたがそうのようなあなたのPoint::getX()Point::getY()機能constを行う必要があり

#include "Point.h" 

using namespace std; 

int main(void) 
{ 
    cout << "\n main has started" << endl; 

    //Point myPoint; 
    Point myPoint(1, 1); 

    myPoint.setPoint(1, 1); 

    cout << "\n The value for X is : " << myPoint.getX() << endl; 
    cout << "\n The value for Y is : " << myPoint.getY() << endl; 

    cout << "\n X scaled by 2 is : " << myPoint.scaleX(2) << endl; 
    cout << "\n Y scaled by 2 is : " << myPoint.scaleY(2) << endl; 

    myPoint.translate(2, 3); 

    cout << "\n main has finished" << endl; 

    return 0; 
} 
+1

あなたは実際に何が間違っているとは言いませんでしたが、 'inline'キーワードが必要なのかどうか確信しています。もしあなたがWii用にコンパイルしていれば、うまくいくでしょう。 – George

+0

どうしてできないのですか?ユークリッド計画の2つのポイント間の距離を計算するアルゴリズム(公式)を知っていますか?それとも実装問題ですか?デバッグのヘルプを求める質問(**はなぜこのコードは動作しないのですか?**)は、必要な動作、_特定の問題_またはエラー、それを再現するのに必要な最短コード**を質問に含める必要があります** 。明確な問題文がない質問は、他の読者にとって有用ではありません。参照:[mcve]を作成する方法。 – YSC

+0

ええ、移植です。私は値から関数を取得することについて聞いたが、私はそれの周りに私の頭を包むことはできません。 –

答えて

3

inline double Point::getX() const 
{ 
    return theX; 
} 

を、彼らはconstない場合はパラメータがあるとき、あなたはそれらを呼び出すことはできません参照番号:const

次に距離は(voidからdoubleへの復帰を変更)されます。

double distance(const Point & aPoint) const 
{ 
    const double x_diff = getX() - aPoint.getX(); 
    const double y_diff = getY() - aPoint.getY(); 
    return std::sqrt(x_diff * x_diff + y_diff * y_diff); 
} 

指数は、あなたはまた、std::sqrtため<cmath>を含める必要2. あるので、私は意図的std::powを使用していません。

関連する問題