I have done this code 5 times in my program for different classes and variables and it has worked fine for 4 of them but the 5th is giving a segmentation fault.
The error occurs just after the cout saying 'here' in the main.cpp code below
I have run valgrind and the result says conditional jump or move depends on uninitalised values
but i cannot see what it is referring.
Could someone please point out where my mistake is and how to fix it.
smurfVillage.h
#ifndef smurfVillage_H
#define smurfVillage_H
#include <string>
using namespace std;
class GenericBuilding
{
private:
string building_name;
string location;
string functionality;
public:
GenericBuilding();
GenericBuilding(string,string,string);
~GenericBuilding();
string getBuildingName();
string getLocation();
string getFunctionality();
string setBuildingName(string);
string setLocation(string);
string setFunctionality(string);
};
#endif
smurfVillage.cpp
//***********************GenericBuilding Constructors***********************//
GenericBuilding::GenericBuilding()
{
//default parameterless constructor
building_name = "Building";
location = "City";
functionality = "Building";
std::cout << " >> Constructing default GenericBuilding" << std::endl;
}
GenericBuilding::GenericBuilding(string Name, string Location, string Functionality)
{
//Constructor for specified values
building_name = Name;
location = Location;
functionality = Functionality;
std::cout << " >> Constructing GenericBuilding" << std::endl;
}
//*******************End of GenericBuilding Constructors********************//
//************************GenericBuilding Destructor************************//
GenericBuilding::~GenericBuilding()
{
std::cout << " >> Destructing GenericBuilding" << std::endl;
std::cout << std::endl;
}
//********************End of GenericBuilding Destructor*********************//
//**********************GenericBuilding get functions***********************//
string GenericBuilding::getBuildingName()
{
return building_name;
}
string GenericBuilding::getLocation()
{
return location;
}
string GenericBuilding::getFunctionality()
{
return functionality;
}
//******************End of GenericBuilding get functions********************//
//**********************GenericBuilding set functions***********************//
string GenericBuilding::setBuildingName(string Name)
{
building_name = Name;
}
string GenericBuilding::setLocation(string Location)
{
location = Location;
}
string GenericBuilding::setFunctionality(string Functionality)
{
functionality = Functionality;
}
//*******************End of GenericBuilding set functions*******************//
main.cpp
#include "smurfVillage.h"
#include <iostream>
using namespace std;
int main ()
{
GenericBuilding *GB1 = new GenericBuilding();
GenericBuilding *GB2 = new GenericBuilding("Hospital", "City", "Caring");
std:cout << "GB2 Name: " << GB2->getBuildingName() << std::endl;
std::cout << "GB2 Location: " << GB2->getLocation() << std::endl;
std::cout << "GB2 Functionality: " << GB2->getFunctionality() << std::endl;
std::cout << "here" << endl;
GB2->setBuildingName("Bank");
GB2->setLocation("Town");
GB2->setFunctionality("Withdraweral");
std::cout << GB2->getBuildingName() << std::endl;
std::cout << GB2->getLocation() << std::endl;
std::cout << GB2->getFunctionality() << std::endl;
delete GB1;
delete GB2;
return 0;
}