Program received signal SIGSEGV, Segmentation fault.
程序的结构是这样的
Person.h 文件
1 #include <string>
2
3 class Age;
4
5 class Person
6 {
7 public:
8
9 virtual ~Person(){}; // see Item 7 for why this is virtual
10
11 Person():name("ImplName"),address("ImplAddress"){};
12
13 Person(std::string name,std::string address)
14 {
15 Person::name = name ;
16 Person::address = address;
17 };
18
19 const std::string& getPersonName() const ;
20
21 int getAge() const;
22 void setAge(const Age& a);
23
24
25 private:
26 std::string name;
27 std::string address;
28
29
30 int age;
31
32 };
33
34 class Student:public Person
35 {
36 public:
37 //Student(){}; // parameters again omitted
38 ~Student(){};
39
40 Student():Person::Person(),schoolName("ImplSchoolName"),schoolAddress("ImplSchoolAddress")
41 {
42
43 };
44
45 Student(std::string schoolName,std::string schoolAddress)
46 {
47 Student::schoolName = schoolName;
48 Student::schoolAddress = schoolAddress;
49
50 };
51
52 bool validateStudent(const Student& s);
53
54 private:
55 std::string schoolName;
56 std::string schoolAddress;
57 };
58
59 class Age
60 {
61 public:
62 int age;
63 };
2
3 class Age;
4
5 class Person
6 {
7 public:
8
9 virtual ~Person(){}; // see Item 7 for why this is virtual
10
11 Person():name("ImplName"),address("ImplAddress"){};
12
13 Person(std::string name,std::string address)
14 {
15 Person::name = name ;
16 Person::address = address;
17 };
18
19 const std::string& getPersonName() const ;
20
21 int getAge() const;
22 void setAge(const Age& a);
23
24
25 private:
26 std::string name;
27 std::string address;
28
29
30 int age;
31
32 };
33
34 class Student:public Person
35 {
36 public:
37 //Student(){}; // parameters again omitted
38 ~Student(){};
39
40 Student():Person::Person(),schoolName("ImplSchoolName"),schoolAddress("ImplSchoolAddress")
41 {
42
43 };
44
45 Student(std::string schoolName,std::string schoolAddress)
46 {
47 Student::schoolName = schoolName;
48 Student::schoolAddress = schoolAddress;
49
50 };
51
52 bool validateStudent(const Student& s);
53
54 private:
55 std::string schoolName;
56 std::string schoolAddress;
57 };
58
59 class Age
60 {
61 public:
62 int age;
63 };
在15,16 47,48 行改成this-> 这样,不能用那个Person:: ,
啊啊,太崩溃了。这样改了以后
Program received signal SIGSEGV, Segmentation fault. 就不见了.
Person.cpp 文件
#include "Person.h"
const std::string& Person::getPersonName() const
{
return this->name;
}
void Person::setAge(const Age& a)
{
age = a.age;
}
const std::string& Person::getPersonName() const
{
return this->name;
}
void Person::setAge(const Age& a)
{
age = a.age;
}
main 函数
Person eg;
const string& a = eg.getPersonName() ;//eg.address;
Age egAge;
eg.setAge(egAge);
const string& a = eg.getPersonName() ;//eg.address;
Age egAge;
eg.setAge(egAge);
发现了一个很奇怪的问题:
在c-free 这个IDE 里,我们要在Main 里 include "Person.cpp"
但是在Code Blocks 里,我们要在Main 里include "Person.h" 搞不懂,真的搞不懂.

浙公网安备 33010602011771号