c++ 動態陣列的建構函數

問題:point.h建構函數內容不知道該怎麼寫

////main.cpp之內容如下

#include <iostream>

using namespace std;

#include "point.h"

int main()

{

cPoint *p1;

p1=new cPoint[10];

p1[2].X(100.0);

delete [] p1;

return 0;

}

-----------------------------------

/////point.cpp內容如下

#include "point.h"

void cPoint::Reset(){x=y=0;}

void cPoint::Move(double xx,double yy){x+=xx;y+=yy;}

---------------------------------------------------------------------------------

////point.h內容如下

#ifndef POINT_H

#define POINT_H

#include <iostream>

using namespace std;

class cPoint

{

private:

double x,y;

public:

cPoint(int n){} ///////////這行不知道該怎麼寫,會寫個int n是因為我想說應該要先傳入陣列大小整數n///////////////////

cPoint(cPoint& p){x=p.x;y=p.y; cout<<"Copy constructor called.\n";}

~cPoint(){cout<<"Destructor called.\n";}

void Reset();

void Move(double xx,double yy);

void SetX(double xx){x=xx;}

void SetY(double yy){y=yy;}

double X(){return(x);}

double Y(){return(y);}

};

#endif // POINT_H

2 個解答

評分
  • 5 年前
    最佳解答

    怎差醬多啊?

    妳班級的關念 是有差喔!

    陣列是陣列.與cPoint這個班級有神關係啊?

    而且妳的cPoint也缺了default constructor

    妳很糟糕 快要被老師打屁股了!!!

    2015-06-15 06:48:49 補充:

    cPoint(cPoint& p){x=p.x;y=p.y; cout<<"Copy constructor called.\n";} // 錯錯錯

    cPoint(cPoint& p):x(p.x),y(p.y){} // 醬才對!!!

    2015-06-17 23:53:11 補充:

    > 還是跑不出來QQ

    跑不出來的原因醬多.要我猜嗎?

    2015-06-18 01:57:19 補充:

    #include<math.h>

    #include<iostream>

    using namespace std;

    template<class T>

    class P{

    public:

    T x,y;

    P():x((T)0),y((T)0){}

    P(T x0,T y0):x(x0),y(y0){}

    ~P(){}

    T dist(P<T> &p){T x1=(x-p.x),y1=(y-p.y);return (T)sqrt(x1*x1+y1*y1);}

    T GetX(){return x;}

    T GetY(){return y;}

    T SetX(T xx){return x=xx;}

    T SetY(T yy){return y=yy;}

    void mv(P<T>p){x+=p.x;y+=p.y;}

    void prt(){cout<<"point("<<x<<','<<y<<") ";}

    };

    template<class T>

    class TR{ // triangle

    P<T> p0,p1,p2;

    public:

    TR(P<T>p00,P<T>p11,P<T>p22):p0(p00),p1(p11),p2(p22){}

    TR(T x0,T y0,T x1,T y1,T x2,T y2):p0(P<T>(x0,y0)),p1(P<T>(x1,y1)),p2(P<T>(x2,y2)){}

    ~TR(){}

    void prt(){}

    T area(){

    T a=0.50*(p0.x*(p1.y-p2.y)+p1.x*(p2.y-p0.y)+p2.x*(p0.y-p1.y));

    return (a>=0)?(a):(-a);

    }

    bool isRight(){

    T d0=p0.dist(p1),d2=p0.dist(p2),d1=p2.dist(p1);

    if(d0>d1&&d0>d2) return d0 == sqrt(d1*d1+d2*d2);

    if(d1>d0&&d1>d2) return d1 == sqrt(d0*d0+d2*d2);

    if(d2>d0&&d2>d1) return d2 == sqrt(d0*d0+d1*d1);

    return false;

    }

    };

    int main(int gc, char *gv[]) {

    P<double> p0(0, 2.0),p1(2.0,1.0);

    p0.prt();

    cout<<"distance to ";p1.prt();cout<<" is " << p0.dist(p1)<<endl;

    TR<double> p2(0.23,0,1,0,0,2);

    cout<<"area of p2 is "<<p2.area()<<endl;

    cout<<"p2 is "<<(p2.isRight()?"":"not ")<<"a right triangle." <<endl;

    p0.mv(P<double>(3.5,2.8));

    p0.prt();

    P<int> p10[10];

    cout<<endl<<"instantiated an array of 10 points. They are:"<<endl;

    for(int i=9;i>=0;--i){ p10[i].prt(); cout<<endl;}

    return 0;

    }

    2015-06-18 22:11:43 補充:

    > 抱歉 執行的時候是顯示 (X)

    抱歉 編譯的時候是顯示 (O)

    > line9 error:no matching function for call to 'cPoint::cPoint()'

    我不是說過了? 你是需要default constuctor?

    > line10 error:no matching function for call to 'cPoint::X(double)'

    比較一下妳的

    X() method 好嗎?

  • 5 年前

    default constructor應該是因為我還沒念到那邊

    所以課本上的範例內容上可能就沒放

    這是課本上原本的例子cPoint內容

    cPoint(double xx,double yy){x=xx;y=yy;cout<<"Constructor called.\n";}

    cPoint(cPoint& p):{x=p.x;y=p.y; cout<<"Copy constructor called.\n";}

    上面cPoint(int n){} ..是我自己亂改的QQ

    不過照大大的改法 還是跑不出來QQ

    2015-06-18 13:39:23 補充:

    抱歉 執行的時候是顯示

    line9 error:no matching function for call to 'cPoint::cPoint()'

    line9是:p1=new cPoint[10];

    line10 error:no matching function for call to 'cPoint::X(double)'

    line10是:p1[2].X(100.0);

還有問題?馬上發問,尋求解答。