我一直得到一个错误,我无法弄清楚如何解决

时间:2014-04-13 22:27:09

标签: c++ struct

这是我的一个课程中的编程作业。我应该根据与参考点的距离来获取点列表并对它们进行排序。提示说使用结构来存储每个点的x,y,z值,并使用另一个结构来存储点和点数。当我尝试编译说

时出错
Points.h:6:2: error: 'Point' does not name a type
Points.cpp: In function 'Points* readPoints(const char*)':
Points.cpp:25:11: error: 'struct Points' has no member named 'pointsarray'

导致此错误的原因是什么,我该如何解决? 有四个文件,Points.h,Point.h,Points.cpp,Point.cpp。

这是Points.h的复制和粘贴:'

#if !defined POINTS
#define POINTS

struct Points
{
    Point** pointsarray;
    int num_points;
};
Points* readPoints(const char file_name[]);
void destroyPoints(Points* pointsarray);
void DisplayPoints(Points* pointsarray);






#endif

这是Points.cpp的副本:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
#include "Points.h"
#include "Point.h"

Points* readPoints(const char file_name[])
{
    ifstream input_file;
    input_file.open(file_name);
    int ARRAY_SIZE = 0;
    input_file >> ARRAY_SIZE;
    int i = 0;
    double x = 0;
    double y = 0;
    double z = 0;
    Points* points;
    points->num_points = ARRAY_SIZE;
    for(i = 0; i < ARRAY_SIZE; i++){
        input_file >> x;
        input_file >> y;
        input_file >> z;
        Point* point = createPoint(x,y,z);
        points->pointsarray[i] = point;
    }
    return points;
}

这是Point.cpp:

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
#include "Point.h"
#include "Points.h"

#if !defined NULL
#define NULL = 0
#endif


Point* createPoint(double x, double y, double z)
{
    Point* point;
    point.x = x;
    point.y = y;
    point.z = z;
    return point;
}

void destroyPoint(Point* point)
{
    delete point;
}

void displayPoint(Point* point)
{
    cout << setprecision(3) << fixed << "(" << point->x << ", " << point->y << ", " << point->z << ")" << endl;
}

这里是point.h

#if !defined POINT
#define POINT

struct Point
{
    double x;
    double y;
    double z;
};

Point* createPoint(double x, double y, double z);
void destroyPoint(Point* point);
void displayPoint(Point* point);
#endif

我非常感谢你能给我的任何解决方案,提前谢谢。

1 个答案:

答案 0 :(得分:0)

你有:

struct Points
{
    Points** pointsarray;
    int num_points;
};

也许你的意思是:

struct Points
{
    Point** pointsarray;
    int num_points;
};

否则,pointsarrayPoints*的数组,而不是Point*的数组。这就是编译器不喜欢这个语句的原因:

    points->pointsarray[i] = point;

该行的RHS方面是Point*,而不是Points*