C++ 我一直遇到一个我不知道如何修复的错误
这是我的一门课上的编程作业。我应该得到一个点列表,并根据与参考点的距离对它们进行排序。提示显示使用一个结构存储每个点的x、y、z值,并使用另一个结构存储点和点数。我试图编译时出错C++ 我一直遇到一个我不知道如何修复的错误,c++,struct,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 mem
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
这是点的复制和粘贴。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;
}
如果您能提供任何解决方案,我将不胜感激,提前谢谢。您有:
struct Points
{
Points** pointsarray;
int num_points;
};
也许你的意思是:
struct Points
{
Point** pointsarray;
int num_points;
};
否则,pointsarray
是点的数组*
而不是点的数组*
。这就是编译器不喜欢该语句的原因:
points->pointsarray[i] = point;
该行的右侧是一个
点*
,而不是点*
您的“点”结构应该包含“点”而不是“点”-我希望这是可以理解的:)这段代码读起来很痛苦。你的老师做得不是很好。为什么Points*Points=新分数代码>而不仅仅是点代码>?你的代码似乎以一种最复杂、最令人困惑的方式处理事情,整个代码都是一团糟。我发现的一个错误是没有分配points->pointsarray。分配点并不会为成员变量分配空间。您可能应该只使用向量
,而不是结构。您可以执行类似于typedef vector Points
的操作,将点更改为点会产生错误:“Points.h:6:2:error:'Point'不命名类型”,“Points.cpp:In function'Points*readPoints(const char*):”“Points.cpp:25:11:error:'struct Points'没有名为'pointsarray'的成员”@user3530038您能用更新后的代码和编译器的错误消息更新您的帖子吗?另外,Points.h
应该#包括“Point.h”
@Matt McNabb,它修复了它!
struct Points
{
Point** pointsarray;
int num_points;
};
points->pointsarray[i] = point;