C++ c++;类内向量-程序segfault

C++ c++;类内向量-程序segfault,c++,gcc,c++11,vector,C++,Gcc,C++11,Vector,该程序编译asis。以及记录的断层 /* * Testing of Vectors. * Uses c++11 standard. * gcc version 4.7.2 * compile with : g++ -std=c++11 -o vec vec.c++ */ #include <iostream> #include <string> #include <vector> #include <stdio.h> #include

该程序编译asis。以及记录的断层

/* 
 * Testing of Vectors.
 * Uses c++11 standard.
 * gcc version 4.7.2
 * compile with : g++ -std=c++11 -o vec vec.c++
 */

#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <unistd.h>

using namespace std;
这门课是我遇到问题的地方

/* declare NamesVector class. */
class NamesVector {
                std::vector<Name *> person_list;
        public:

        NamesVector(void);
        ~NamesVector(void);
        virtual Name *getPerson(void);
        virtual void addPerson(Name *);
        virtual void Print(void);
        virtual void FindPerson(std::string);
};

/* adds person to vector/list */
void NamesVector::addPerson(Name *n){
        person_list.insert(person_list.begin(), n);
};

/* prints all persons */
void NamesVector::Print(){
        for (auto v: person_list){
                std::cout << v->GetFirstName() <<
                 " " << v->GetLastName() << std::endl;
        }
};

/* main() */
int main(int argc, char **argv){
/*声明NamesVector类*/
类名称向量{
std::矢量人_列表;
公众:
名称向量(void);
~n向量(void);
虚拟名称*getPerson(无效);
虚拟人(姓名*);
虚拟作废打印(void);
虚拟void FindPerson(std::string);
};
/*将人员添加到向量/列表中*/
void NamesVector::addPerson(名称*n){
person\u list.insert(person\u list.begin(),n);
};
/*打印所有人*/
void NamesVector::Print(){
用于(自动v:人员列表){

std::cout GetFirstName()
peopleList
仅被声明,但尚未被实例化。因此,您会遇到分段错误,因为您试图取消引用未初始化的指针。您需要改为执行
NameVector*peopleList=new NameVector();


当然,要做到这一点,您必须为NamesVector定义一个默认构造函数。您可能已经在某个地方定义了该构造函数,但您没有在上面显示它。(这可能就是您在尝试该操作时遇到所述编译器错误的原因。)

您必须为类NamesVector定义一个构造函数和析构函数:

// constructor    
NamesVector::NamesVector() {

    // create an instance of the vector
    person_list = new Vector<Name *>();

}

// destructor
NamesVector::~NamesVector() {

    // delete the instance of the vector
    delete person_list;
}

问:您是否尝试过通过调试器遍历编译clean(但仍然是segfaults)的版本?您是否验证了“person”和“people”在您设置segfaults时都是有效的对象?
        /* pointer to person list */
        NamesVector *peopleList;

        /* pointer to a person */
        Name *person;

        /* instanseate new person */
        person = new Name("Joseph", "Heller");

        /* works ok */
        std::cout << person->GetFirstName() << " " 
        << person->GetLastName() << std::endl;
        /* segfaults - Why!?! - insert into peopleList vector */
        peopleList->addPerson(person);      

        peopleList->Print();
        std::cout << std::endl << std::endl;

        return EXIT_SUCCESS;
}
// constructor    
NamesVector::NamesVector() {

    // create an instance of the vector
    person_list = new Vector<Name *>();

}

// destructor
NamesVector::~NamesVector() {

    // delete the instance of the vector
    delete person_list;
}
NamesVector *nv= new NamesVector().