C ++传递一个类数组

时间:2013-09-18 02:03:22

标签: c++ arrays class

任何人都可以帮助我传递一组类的语法 去另一个班级。将类数组传递给另一个类的语法 上课让我挨打。 class line尝试由a初始化 点数组,但原型不匹配。

#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)'的未定义引用

3 个答案:

答案 0 :(得分:2)

您需要为线类定义构造函数 - 您只提供了一个声明。

#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;
}

我建议您查看definitions and declarations之间的区别。此外,您应该考虑在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;
};

答案 1 :(得分:0)

您没有为构造函数提供定义。

尝试:

line(point *points, int np) : coords(points), numpoints(np) {}

答案 2 :(得分:0)

缺少构造函数“line”的主体。 您只定义原型。

相关问题