[英]Program received signal SIGSEGV, Segmentation fault
Ok... I am ripping my hair out... Why am I getting segmentation fauls when I am passing a string called "name" with contents "joel" into好的......我正在扯掉我的头发......当我将一个名为“name”的字符串传递给内容“joel”时,为什么会出现分段错误
void person::setName(string newName)
{
personName = newName;
}
Header file: Header 文件:
class person {
public:
int getID();
string getName();
void setID(int newID);
void setName(string newName);
private:
int personID;
string personName;
};
btw... the function call is by a child, although I dont see how that could cause an issue.顺便说一句... function 电话是由一个孩子来的,尽管我不明白这会如何导致问题。
If you are on Linux, try running valgrind .如果您使用的是 Linux,请尝试运行valgrind 。 You just compile with
-g
(with gcc), and then run your program with valgrind
in front:您只需使用
-g
(使用 gcc)进行编译,然后在前面使用valgrind
运行您的程序:
$ valgrind myprogram
Unlike the GCC solutions, which tell you when the segfault occurs, valgrind usually tells you exactly when the first memory corruption occurs, so you can catch the problem much closer to its source.与 GCC 解决方案不同,后者会告诉您何时发生段错误,而 valgrind 通常会准确地告诉您第一次 memory 损坏发生的时间,因此您可以更接近问题的根源。
PS. PS。 It rhymes with "flint", not "find".
它与“flint”押韵,而不是“find”。
Probably, you are dereferencing a rogue pointer.可能,您正在取消引用流氓指针。 By pure guesswork, have you got something like this, perhaps:
通过纯粹的猜测,你有没有这样的东西,也许:
Person persons[10];
for (i=1; i<=10; i++)
persons[i].setName("joel");
The problem might be:问题可能是:
for (i=0; i<10; i++)
for (i=0; i<10; i++)
There could literally be hundreds of other causes, but since I don't have your code, this is my attempt to guess most plausible errors;)实际上可能有数百个其他原因,但由于我没有你的代码,这是我试图猜测最合理的错误;)
( Note to self: why am I doing this/I'm not psychic? ) (自我注意:我为什么要这样做/我不是通灵者? )
The code looks fine apart from the fact that you copying string all the time.除了您一直在复制字符串这一事实之外,该代码看起来还不错。 Instead of
代替
void setName(string newName);
should be应该
void setName(const string& newName);
The issue must be in the method invocation.问题必须在方法调用中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.