1

クラスの配列を別のクラスに渡す構文について誰か助けてくれませんか。クラスの配列を別のクラスに渡す構文には、私は打ちのめされました。クラス行は点の配列で初期化しようとしましたが、プロトタイプが一致しません。

#include    <iostream>
using namespace std;
class point {
public:
    point() {}
    point(int x, int y) : X(x), Y(y) {}
    void setXY(int x, int y) { X = x; Y = y; }
    int getX() { return X; }
    int getY() { return Y; }
private:
    int X, Y;
};
class line {
public:
    line(point *points, int);  // Problem line.
private:
    point *coords;
    int numpoints;
};
int main() {
    point   points[3];
    points[0].setXY(3, 5);
    points[1].setXY(7, 9);
    points[2].setXY(1, 6);

    line    l(points, 3);    // Problem line.
    return 0;
}

エラー メッセージ: cygdrive/c/Tmp/cc4mAXRG.o:a.cpp:(.text+0xa7): 「line::line(point*, int)」への未定義の参照

4

3 に答える 3

2

line クラスのコンストラクターを定義する必要があります - 宣言を提供しただけです。

#include    <iostream>
using namespace std;
class point {
public:
    point() {}
    point(int x, int y) : X(x), Y(y) {}
    void setXY(int x, int y) { X = x; Y = y; }
    int getX() { return X; }
    int getY() { return Y; }
private:
    int X, Y;
};
class line {
public:
    line(point *points, int count)
     : coords(points), numpoints(count) {}
private:
    point *coords;
    int numpoints;
};
int main() {
    point   points[3];
    points[0].setXY(3, 5);
    points[1].setXY(7, 9);
    points[2].setXY(1, 6);

    line    l(points, 3);
    return 0;
}

定義と宣言の違いを確認することをお勧めします。さらに、ポイントを管理するためにクラスに を維持することを検討する必要がありstd::vector<point>ます。line回線クラスは次のように動作します。

#include <vector>
class line {
public:
    line(std::vector<point> points)
     : coords(points), numpoints(coords.size()) {}
private:
    std::vector<point> coords;
    int numpoints;
};
于 2013-09-18T02:05:45.353 に答える
0

コンストラクターの定義を提供していません。

試す:

line(point *points, int np) : coords(points), numpoints(np) {}
于 2013-09-18T02:05:45.320 に答える
0

コンストラクター "line" の本体がありません。プロトタイプのみを定義します。

于 2013-09-18T02:09:16.697 に答える