7 Replies - 2542 Views - Last Post: 04 April 2012 - 05:27 PM Rate Topic: -----

#1 Crakrjack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-April 12

segmentation fault (core dumped)

Posted 04 April 2012 - 07:09 AM

Hi all, I'm very new to c++ programing so hoping you can help with something.
I am writing a program with several classes, a number of the classes create vectors of the other classes. This info probably isn't needed to help with my question but there it is.
The problem seems to be in the Stack.h file which is here
#ifndef STACK_H
#define STACK_H

#include <iostream>
#include <string>
#include <vector>
#include "Shelf.h"

using namespace std;

class Stack
{
private:
	int stackWeight;   //changing this to double causes a seg fault?
	int numOfShelves;
	string firstBook;
	string lastBook;
	int numOfBooks;
	vector<Shelf> shelves;				//create a vector of shelves

public:	
	
	Stack();
	//~Stack();			//deconstructor
	int getNumOfBooks();
	double getStackWeight();		
	string getFirstBook();
	string getLastBook();
	void addShelf();
	void removeShelf();
	void addBookToStack(string s, string i, string t, string y);
	
};
#endif


the constructor implementation is
Stack::Stack()			//Stack constructor
{
	cout<<"stack created\n";
	shelves.push_back(Shelf());		//add 2 shelves
	shelves.push_back(Shelf());
	stackWeight = 2.0; 		//weight of 2 empty shelves.
	numOfShelves=2;
	firstBook="null";
	lastBook="null";
}


I'm early stages with writing this so none of the other functions are being called.
This all compiles and runs ok, up to this point, but I really need the private variable stackWeight to be of type double but for some reason this change is resulting in the error 'segmentation fault (core dumped)'
I have no idea why this is happening, any tips would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: segmentation fault (core dumped)

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,349
  • Joined: 25-December 09

Re: segmentation fault (core dumped)

Posted 04 April 2012 - 07:16 AM

The code you posted looks correct, your problem is probably in some other area. Post the smallest complete program that illustrates your problem.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Crakrjack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-April 12

Re: segmentation fault (core dumped)

Posted 04 April 2012 - 07:53 AM

Thanks so much for your quick response. I took all the code i was using from the various .h and .cpp files and stuck them into a single .cpp file in order to post it here, but it works fine like that. I'll post the code anyway and if you consider that there are separate .h and .cpp files for each class, the error might be evident?
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

class Book
{
private:
	string author;
	string initials;
	string title;
	string yearPub;
	double weight;

public:
	Book(string s, string i, string t, string y);
	string getAuthor();
	string getInitials();
	string getTitle();
	string getYearPub();
	double getWeight();
	
};
Book::Book(string a, string i, string t, string y) //concatonate authour and initials?
{
	author=a;
	initials=i;
	title=t;
	yearPub=y;				//will use sstream to convert string to int.
	double year;
	stringstream(y)>>year;
	weight=year/10000;		//weight in kg.
}
string Book::getAuthor(){return author;}
string Book::getInitials(){return initials;}
string Book::getYearPub(){return yearPub;}
double Book::getWeight(){return weight;}

class Shelf
{
private:
	int booksWeight;
	double shelfWeight;
	vector<Book> books;		//create empty vector of books
	
public:
	Shelf();
	int getNumOfBooks();
	double getShelfWeight();
	void addBookToShelf();
};

Shelf::Shelf()
{
	cout<<"\tshelf created\n";
	booksWeight=0;
}

class Stack
{
private:
	double stackWeight; 	  //changing this to double causes a seg fault?
	vector<Shelf> shelves;				//create a vector of shelves

public:	
	
	Stack();
	double getStackWeight();		
	void addBookToStack(string s, string i, string t, string y);
};
Stack::Stack()			//Stack constructor
{
	cout<<"Stack created\n";
	shelves.push_back(Shelf());		//add 2 shelves
	shelves.push_back(Shelf());
	stackWeight = 2.1; 		//weight of 2 empty shelves.
	cout<<"Stack weight is "<<stackWeight<<endl;
}
void Stack::addBookToStack(string s, string i, string t, string y)
{
	double year;
	stringstream(y)>>year;
	double weight=year/10000;		//weight in kg.
}

class Library
{
private:
	int numOfBooks;
	vector<Stack> stacks;
public:
	Library();
	~Library();
	int getNumOfBooks();
	void addStack();
	void addBook(string s, string i, string t, string y);
};
Library::Library()		//Library constructor
{
cout<<"Library created\n";
stacks.push_back(Stack());		//adds 3 stacks
stacks.push_back(Stack());
stacks.push_back(Stack());
}
void Library::addBook(string s, string i, string t, string y)	//find where it belongs add it to vector
{
	stacks[0].addBookToStack(s,i,t,y);							// adds book to the first stack
}

int main()
{
Library* TheLibrary = new Library();	//creates library
TheLibrary->addBook("Rees", "C.A.", "The Complete works of Chris", "1971");
}

At this stage I'm simply creating a library, with 3 stacks each with 2 shelves, the addBook function is called in Library and Stack but doesn't really do anything yet.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,349
  • Joined: 25-December 09

Re: segmentation fault (core dumped)

Posted 04 April 2012 - 08:04 AM

The code you provided runs, and produces output for me:

Quote

Library created
Stack created
shelf created
shelf created
Stack weight is 2.1
Stack created
shelf created
shelf created
Stack weight is 2.1
Stack created
shelf created
shelf created
Stack weight is 2.1


Why are you trying to dynamically create your instance? Wouldn't this work just as well?
int main()
{
   Library TheLibrary;
   TheLibrary.addBook("Rees", "C.A.", "The Complete works of Chris", "1971");
}



Also if you define a destructor in your class you need to implement the function. If you don't define a destructor the default will be created by the compiler.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 Crakrjack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-April 12

Re: segmentation fault (core dumped)

Posted 04 April 2012 - 04:42 PM

Thanks for your help, it seems to be working now, I'm not sure if that's a good thing because i didn't really change anything, perhaps it will come back to bite me later?
As for dynamically creating my instance, I'm trying to get the hang of pointers.
I studied first year comp sci at uni where they taught Java, then had a year with no comp sci courses (I'm doing a double degree) and in that year they changed the curriculum to focus on c++ rather than Java and I'm now in a second year course studying c++ with no real knowledge of it.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,349
  • Joined: 25-December 09

Re: segmentation fault (core dumped)

Posted 04 April 2012 - 04:46 PM

If you are trying to get used to pointers I suggest you start simpler. Start with a POD type and create an array using dynamic memory. In my opinion trying to learn pointers using a class or structure is more complicated because of the use of the constructors and destructors. Start simple.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 Crakrjack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-April 12

Re: segmentation fault (core dumped)

Posted 04 April 2012 - 04:58 PM

I used pointers in my first assignment, which simply checked input and determined if it was a palindrome. I believe this is what you'd call a POD, no classes, just a few functions and a main statement.I should revise that.
For now, I'm just trying to keep up with the workload and I'm using a bit of trial and error rather than taking the time to understand fully whats going on. Not a good move I know. I'm on break at the moment so if i get through this assignment I will revise and view a few tutorials.

Thanks again, Chris.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,349
  • Joined: 25-December 09

Re: segmentation fault (core dumped)

Posted 04 April 2012 - 05:27 PM

POD stands for Plain Old Data and is used to refer to the standard data types, char, integer types, and floating point types.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1