C++ C++;-结构指针数组
我有一个小程序,其中有两个结构: Person-由id和基本方法组成 Ppl-由一群人组成,他们有一些方法来操作数组C++ C++;-结构指针数组,c++,arrays,pointers,struct,C++,Arrays,Pointers,Struct,我有一个小程序,其中有两个结构: Person-由id和基本方法组成 Ppl-由一群人组成,他们有一些方法来操作数组 struct Person { const int id; Person(); Person(int a); }; Person::Person(int a) : id(a) {} 这是Person结构及其方法 const int MAX = 5; 设置数组长度的限制 struct Ppl { static int current; //Keeps track
struct Person {
const int id;
Person();
Person(int a);
};
Person::Person(int a) : id(a) {}
这是Person结构及其方法
const int MAX = 5;
设置数组长度的限制
struct Ppl {
static int current; //Keeps track of current position in array
Person *ppls[MAX]; //Creates an array of Person structure pointers
void add(int a); //Adds a person with id to the next available position
//void remove(int b);
int searchid(int c); //Searches ppls for an id c.
Ppl(); //Constructor
};
int Ppl::current = 0; //Initializing static variable
void Ppl::add(int a) {
int ret = searchid(a); //Determine if id a already exists in ppls
//cout << "Ret: " << ret << endl;
if (ret > MAX) { //If value returned is > MAX, value exists
cout << "User " << a << " already exists" << endl;
} else { //else, add it to the next available spot
Person p(a);
ppls[current] = &p;
cout << "Added user: " << a << " at index: " << current << endl;
current++;
}
}
Ppl::Ppl() {
current = 0;
int i = 0;
while (i < MAX) { //Sets all Person pointers to NULL on initialization
ppls[i] = NULL;
cout << "ppls[" << i << "] is NULL" << endl;
i++;
}
}
int Ppl::searchid(int c) {
int i = 0;
bool found = false;
while(i < MAX) {
if (ppls[i] == NULL) { //If NULL, then c wasn't found in array because
break; //there is no NULL between available spots.
} else {
if (ppls[i]->id == c) {
found = true;
}
}
i++;
}
if (found == true) {
return 10; //A number higher than MAX
} else {
return 1; //A number lower than MAX
}
}
为什么它要将所有新条目添加到数组的开头,而将其余条目保持为空?
为什么它没有检测到已经添加了21个呢
我一直疯狂地想弄明白这一点。任何帮助都将不胜感激。
非常感谢社区
编辑
我已经修复了它,这样它就可以将元素添加到数组中,并在id存在时进行识别
我通过添加析构函数对Ppl结构进行了更改:
Ppl::~Ppl() {
int i = 0;
while (i < MAX) {
delete ppls[i];
i++;
}
}
什么是分段错误?如何修复它?
再次感谢
Person p(a);
ppls[current] = &p;
这是个问题。您正在存储指向局部变量的指针。您的程序有未定义的行为
使用
确保删除Ppl
的析构函数中的对象
改进建议
现在还不清楚你的目标是什么,但是你可以通过使用
std::vector<Person> ppls;
这是个问题。您正在存储指向局部变量的指针。您的程序有未定义的行为
使用
确保删除Ppl
的析构函数中的对象
改进建议
现在还不清楚你的目标是什么,但是你可以通过使用
std::vector<Person> ppls;
您正在将堆栈变量的地址分配给指针数组。。。总是个坏主意<代码>人p(a)代码>然后
ppls[当前]=&p代码>?我没有时间写一个合适的答案,但你的编程生涯还有很长的路要走…谢谢。我知道。我仍然只是一个大学生,如果我表现出轻蔑或侮辱的话,我会感到抱歉。很高兴看到有新的人开始编程,很抱歉,我没有时间适当地提供帮助。欢迎来到俱乐部!请阅读,并学习。它们在将来对您将是非常宝贵的。您正在将堆栈变量的地址分配给指针数组。。。总是个坏主意<代码>人p(a)代码>然后ppls[当前]=&p代码>?我没有时间写一个合适的答案,但你的编程生涯还有很长的路要走…谢谢。我知道。我仍然只是一个大学生,如果我表现出轻蔑或侮辱的话,我会感到抱歉。很高兴看到有新的人开始编程,很抱歉,我没有时间适当地提供帮助。欢迎来到俱乐部!请阅读,并学习。它们在将来对你来说将是无价的。谢谢你的帮助。我不能使用向量,这个程序没有明确的目标,是因为我写这个程序是为了理解一些我遇到困难的概念。我们学校教我们如何使用structs,我当时正在做一个练习。我在修正错误后运行了这个程序。我遇到了一个分段错误,请参考我上面的编辑。@这是一个长显示名,分段错误是由main
中的最后一行引起的,因为people.ppls[3]
是NULL
。感谢您的帮助。我不能使用向量,这个程序没有明确的目标,是因为我写这个程序是为了理解一些我遇到困难的概念。我们学校教我们如何使用structs,我当时正在做一个练习。我在修正错误后运行了这个程序。我遇到一个分段错误,请参阅我上面的编辑。@这是一个长显示名,分段错误是由main
中的最后一行引起的,因为people.ppls[3]
是NULL
。
ppls[0] is NULL
ppls[1] is NULL
ppls[2] is NULL
ppls[3] is NULL
ppls[4] is NULL
Added user: 21 at index: 0
21
Added user: 7 at index: 1
21 7
Added user: 42 at index: 2
21 7 42
User 21 already exists
Segmentation fault (core dumped)
Person p(a);
ppls[current] = &p;
Person* p = new Person(a);
ppls[current] = p;
std::vector<Person> ppls;
Person *ppls[MAX];