Rusty
Rusty

Reputation: 389

Insert into multimap causes segfault

I am working on a project using multimaps inside of my own class, and I have run into a segfault. Here are the parts of my code relating to the issue. I would really appreciate some help. Thanks.

Here is database.h

#include <iostream>
#include <map>

using namespace std;

class database{
 public:
  database(); // start up the database                                               
  int update(string,int); // update it                                               
  bool is_word(string); //advises if the word is a word                              
  double prox_mean(string); // finds the average prox                                
 private:
  multimap<string,int> *data; // must be pointer                                     
 protected:

};

Here is the database.cpp

#include <iostream>
#include <string>
#include <map>
#include <utility>

#include "database.h"

using namespace std;


// start with the constructor                                               
database::database()
{
  data = new multimap<string,int>; // allocates new space for the database  
}

int database::update(string word,int prox)
{
  // add another instance of the word to the database                       
  cout << "test1"<<endl;
  data->insert( pair<string,int>(word,prox));
  cout << "test2" <<endl;
  // need to be able to tell if it is a word                                
  bool isWord = database::is_word(word);
  // find the average proximity                                             
  double ave = database::prox_mean(word);

  // tells the gui to updata                                                
  // gui::update(word,ave,isWord); // not finished yet                      

  return 0;
}

Here is test.cpp

#include <iostream>
#include <string>
#include <map>

#include "database.h" //this is my file                                              

using namespace std;

int main()
{
  // first test the constructor                                                      
  database * data;

  data->update("trail",3);
  data->update("mix",2);
  data->update("nut",7);
  data->update("and",8);
  data->update("trail",8);
  data->update("and",3);
  data->update("candy",8);

  //  cout<< (int) data->size()<<endl;                                               

  return 0;

}

Thanks very much. It compiles and runs up to cout << "test1" << endl; but segfaults on the next line.

Rusty

Upvotes: 0

Views: 964

Answers (2)

Thiago Cardoso
Thiago Cardoso

Reputation: 725

You need to allocate your database before starting to insert data on it.

Change:

database *data;

to:

database *data = new database();

or:

database data;

in main().

EDIT: if you use the latter, change -> to . on the subsequent method calls. Otherwise, remember to delete your data object after using it.

Upvotes: 1

Mark B
Mark B

Reputation: 96241

You never actually created a database object, just a pointer to nowhere (perhaps you're used to another language).

Try creating one like this database data;

And then change your -> to . to access the members.

Consider obtaining one of the books at The Definitive C++ Book Guide and List as well.

Upvotes: 5

Related Questions