Reputation: 1
The program compiles asis. And segfaults where noted.
/*
* 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;
This class works fine.
/* declare Person class. */
class Name {
std::string first;
std::string last;
public:
Name(void);
Name(std::string first, std::string last){
this->first = first;
this->last = last;
}
~Name();
std::string GetFirstName(){
return this->first;
}
std::string GetLastName(){
return this->last;
}
};
This class is where I'm having problems.
/* 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){
I've tried : NamesVector *nv = new NamesVector() here and all it gives is the error: 'undefined reference to `NamesVector::NamesVector()' while compiling.
In addition to this I've tried replacing:
NamesVector *peopleList; with NamesVector peopleList;
(And made appropriate changes to code where needed.)
And got the following errors at compile time:
undefined reference to `NamesVector::NamesVector()'
undefined reference to `NamesVector::~NamesVector()
/* 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;
Here is where the program segfaults. Any ideas?
/* segfaults - Why!?! - insert into peopleList vector */
peopleList->addPerson(person);
peopleList->Print();
std::cout << std::endl << std::endl;
return EXIT_SUCCESS;
}
Upvotes: 0
Views: 509
Reputation: 2093
You have to define a constructor and a destructor for class 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;
}
When you define the constructor and destructor, you should be able to call:
NamesVector *nv= new NamesVector().
Upvotes: 1
Reputation: 4490
peopleList
has merely been declared but hasn't been instantiated. Therefore, you get a segmentation fault because you are trying to dereference an uninitialized pointer. You need to do NamesVector *peopleList = new NamesVector();
instead.
Of course, to do that, you will have to define a default constructor for NamesVector. You may already have that defined somewhere but you didn't show it above. (And that's probably why you get the compiler error you mentioned when you try that.)
Upvotes: 0