I get stuck for this one for 3-4 hours, try various sources but no help, would you please point out what is wrong with my code ? This is the first time I am using a pointer.This one is the error : "Segmentation fault (core dumped)" when compiling
// Integer.cpp
#include <cstddef>
#include "Integer.h"
Integer::Integer( int intVal )
{
*value = intVal;
}
Integer::Integer(const Integer& other)
{
//copy constructor
*value=*(other.value);
}
//deconstructor
Integer::~Integer(){
if(value != NULL) delete value;
}
int Integer::getInteger() const
{
return *value;
}
void Integer::setInteger( int newInteger )
{
*value = newInteger;
}
Integer& Integer::operator=( const Integer& rhInt )
{
*value = *(rhInt.value);
return *this;
}
---------------------------------------------
//Integer.h
#ifndef INTEGER_H
#define INTEGER_H
class Integer
{
public:
Integer() {*value = 0;}
Integer( int intVal );
Integer(const Integer& other);//copy constructor
~Integer(void);
int getInteger() const;
void setInteger( int newInteger );
Integer& operator=( const Integer& rhInteger );
private:
int* value;
};
#endif
-----------------------------------------
// IntegerTest.cpp
#include <iostream>
#include <cstdlib>
#include "Integer.h"
using namespace std;
void displayInteger( char* str, Integer intObj )
{
cout << str << " is " << intObj.getInteger() << endl;
}
int main( int argc, char* argv[] )
{
Integer intVal1;
Integer intVal2(10);
displayInteger( "intVal1", intVal1 );
displayInteger( "intVal2", intVal2 );
//intVal1 = intVal2;
//displayInteger( "intVal1", intVal1 );
return EXIT_SUCCESS;
}