?
Solved

Calling constructors causes seg fault

Posted on 2008-11-03
8
Medium Priority
?
275 Views
Last Modified: 2013-12-14
I have been at this for about 2 hours.  The rest of my code works great.  The problem is that everytime I call the constructor it keeps seg faulting.  I'm thinking this maybe a simple formatting issue.  Does anything look wrong with this?

Thanks,
JOe K.
Student::Student() : studentID(0), credits(0), GPA(0.0){
    // Constructs a default student with an ID of 0, 0 credits, and 0.0 GPA.
		//studentID = 0;
		//credits = 0;
		//GPA = 0.0;
	}
	
    Student::Student(int ID) : studentID(ID), credits(0), GPA(0.0){
    // Constructs a student with the given ID, 0 credits, and 0.0 GPA.
		//studentID = ID;
		//credits = 0;
		//GPA = 0.0;
	}
    Student::Student(int ID, int cr, double grPtAv) : studentID(ID), credits(cr), GPA(grPtAv){
    // Constructs a student with the given ID, number of credits, and GPA.
		printf("%s", ID);
		//studentID = ID;
		//credits = cr;
		//GPA = grPtAv;
	}

Open in new window

0
Comment
Question by:ClaudeWalker
  • 5
  • 3
8 Comments
 
LVL 11

Accepted Solution

by:
Deepu Abraham earned 2000 total points
ID: 22873743
The problem is here in
printf("%s", ID);

it should be
printf("%d",ID);

Best Regards,
DeepuAbrahamK


0
 

Author Comment

by:ClaudeWalker
ID: 22873775
I still get a seg fault just using this

Student::Student(int ID, int cr, double grPtAv) : studentID(ID), credits(cr), GPA(grPtAv){
}
 
//its called from this header
 
#ifndef STUDENT_H
#define STUDENT_H
 
class Student {
 
  private:
 
    int getID() const;       // returns the student ID
    int getCredits() const;  // returns the number of credits
    double getGPA() const;   // returns the GPA
};
 
#endif

Open in new window

0
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 22873877
Here is the code...You could try this one...

Best Regards,
DeepuAbrahamK
//Header file----#include "ee.h"
 
//its called from this header
 
#ifndef STUDENT_H
#define STUDENT_H
 
class Student {
 
private:
	int studentID,credits;
	double GPA;
public:
 
	Student(int ID, int cr, double grPtAv);
  
	int getID() const;       // returns the student ID
    int getCredits() const;  // returns the number of credits
    double getGPA() const;   // returns the GPA
};
 
#endif
 
//---cpp file --ee.cpp
 
#include "stdafx.h"
#include "ee.h"
#include <stdlib.h>
#include <windows.h>
#include <stdio.h>
 
 
Student::Student(int ID, int cr, double grPtAv) : studentID(ID), credits(cr), GPA(grPtAv)
{
}
 
int  Student::getCredits() const
{
	return credits;
}
 
int Student::getID() const
{
	return studentID;
}
 
double Student::getGPA() const
{
	return GPA;
}
 
 
int main()
{
	Student s(1,2,3.3);
	printf("%d",s.getID());
	printf("%d",s.getCredits());
	printf("%f",s.getGPA());
}

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ClaudeWalker
ID: 22873906
Thanks for your help so far.  I'll give you a little more information.  I'm working on a Linux machine and I've got a black box object file calling my constructor for Student.  Every time the constructor is called I get a segmentation fault.

The first  part of the code snippet shows the constructors in the .cpp file, while the second shows the entire .h file.  Any ideas why I'm still getting a segmentation fault?
    Student::Student() : studentID(0), credits(0), GPA(0.0){
		
	}
	
    Student::Student(int ID) : studentID(ID), credits(0), GPA(0.0){
		//printf("%d", studentID);
		
	}
    Student::Student(int ID, int cr, double grPtAv) : studentID(ID), credits(cr), GPA(grPtAv){
		
	}
 
===============================================
 
#ifndef STUDENT_H
#define STUDENT_H
 
/*
 * Student class
 *
 * A Student object contains a student ID, the number of credits, and an
 * overall GPA.
 */
class Student {
 
  public:
    
    Student();
    // Constructs a default student with an ID of 0, 0 credits, and 0.0 GPA.
 
    Student(int ID);
    // Constructs a student with the given ID, 0 credits, and 0.0 GPA.
 
    Student(int ID, int cr, double grPtAv);
    // Constructs a student with the given ID, number of credits, and GPA.
 
    // Accessors
    int getID() const;       // returns the student ID
    int getCredits() const;  // returns the number of credits
    double getGPA() const;   // returns the GPA
 
    // Other methods
 
    void update(char grade, int cr);
    // Updates the total credits and overall GPA to take into account the
    // additions of the given letter grade in a course with the given number
    // of credits.  The update is done by first converting the letter grade
    // into a numeric value (A = 4.0, B = 3.0, etc.).  The new GPA is 
    // calculated using the formula:
    //
    //            (oldGPA * old_total_credits) + (numeric_grade * cr)
    //   newGPA = ---------------------------------------------------
    //                        old_total_credits + cr
    //
    // Finally, the total credits is updated (to old_total_credits + cr)
 
    void print() const;  
    // Prints out the student to standard output in the format:
    //   ID, credits, GPA
    // Note: the end-of-line is NOT printed after the student information 
 
  private:
    int studentID;
    int credits;
    double GPA;
};
 
#endif

Open in new window

0
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 22873965
>>I've got a black box object file calling my constructor for Student.
Do you have the code snippet?  Will you be able to recompile it...? It could be giving a fault within the calling function..

0
 

Author Comment

by:ClaudeWalker
ID: 22873990
The black box code snippet is assumed to work correctly.  Even if I call the constructor explicitly elsewhere, I get the segmentation fault.  I'm at a loss for what to do.  Thanks again for the help guys.
0
 

Author Comment

by:ClaudeWalker
ID: 22873992
P.S. Any other ideas?
0
 

Author Comment

by:ClaudeWalker
ID: 22874107
I figured it out...ended up being a string formatting issue.  Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question