【发布时间】:2015-11-04 02:06:19
【问题描述】:
我有一个小程序,其中有两个结构: 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 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
}
}
主要功能是:
int main() {
Ppl people;
people.add(21);
cout << people.ppls[0]->id << endl;
people.add(7);
cout << people.ppls[0]->id << " ";
cout << people.ppls[1]->id << endl;
people.add(42);
cout << people.ppls[0]->id << " ";
cout << people.ppls[1]->id << " ";
cout << people.ppls[2]->id << endl;
people.add(21);
cout << people.ppls[0]->id << " ";
cout << people.ppls[1]->id << " ";
cout << people.ppls[2]->id << " ";
cout << people.ppls[3]->id << endl;
}
我得到的输出是:
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
7 0
Added user: 42 at index: 2
42 0 0
Added user: 21 at index: 3
21 0 0 0
为什么要将所有新条目添加到数组的开头,而将其余条目保持为 NULL? 为什么没有检测到已经添加了 21。
我一直在疯狂地试图弄清楚这一点。任何帮助将非常感激。 非常感谢社区。p>
编辑 我已对其进行了修复,以便它将元素添加到数组中并识别 id 何时存在。
我通过添加析构函数对 Ppl 结构进行了更改:
Ppl::~Ppl() {
int i = 0;
while (i < MAX) {
delete ppls[i];
i++;
}
}
并通过更改添加方法。
void Ppl::add(int a) {
int ret = searchid(a);
//cout << "Ret: " << ret << endl;
if (ret > MAX) {
cout << "User " << a << " already exists" << endl;
} else {
**Person *p = new Person(a);
ppls[current] = p;**
cout << "Added user: " << a << " at index: " << current << endl;
current++;
}
}
所以现在的输出是
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;?我没有时间写一个正确的答案,但你的编程生涯还有很长的路要走...... -
谢谢。而且我知道。我还只是个本科生
-
对不起,如果我不屑一顾或侮辱了我。很高兴看到新人开始编程,很抱歉我没有时间提供适当的帮助。欢迎加入俱乐部!
-
请阅读this blog post,学习how to use a debugger。它们在未来对您来说将是无价之宝。
标签: c++ arrays pointers struct