Cannot erase a shared_ptr from set - c++

I am trying to have an object with a set of pointers to another object. when I try to erase on of the set's values I get an error and crash, I really dont know what could be causing it. here is the library and after that the main function:
when I try to run it it does everything its supposed to do, and when it gets to the removeemployee it crashes and sends out the following: Process finished with exit code -1073740940 (0xC0000374)
I run it on clion if that matters, and in c++11.
#include <ostream>
#include <iostream>
#include "Manager.h"
Manager::Manager(int id, string firstName, string lastName, int birthYear)
: Citizen(id, firstName, lastName,birthYear), salary(0), employees(), work_flag(false) {}
int Manager::getSalary() const {
return salary;
}
void Manager::setSalary(int _salary) {
if((salary + _salary) < 0){
salary = 0;
}else {
salary += _salary;
}
}
void Manager::addEmployee(Employee* employee_add) {
shared_ptr<Employee> employee(employee_add);
if(employees.find(employee) != employees.end()){
throw mtm::EmployeeAlreadyExists();
}
employees.emplace(employee);
}
//this is the function
void Manager::removeEmployee(int id) {
for(auto it = employees.begin(); it != employees.end(); it++){
if(it->get()->getId() == id){
employees.erase(it);
return;
}
}
throw mtm::EmployeeDoesNotExists();
}
Manager *Manager::clone() {
return new Manager(*this);
}
ostream &Manager::printShort(ostream &os) const {
os<<this->getFirstName()<<" "<<this->getLastName()<<endl;
os<<"Salary :"<<this->getSalary()<<endl;
return os;
}
ostream &Manager::printLong(ostream &os) const {
os<<this->getFirstName()<<" "<<this->getLastName()<<endl;
os<<"id - "<<this->getId()<<" birth_year - "<<this->getBirthYear()<<endl;
os<<"Salary :"<<this->getSalary()<<endl;
os<<"Employees:"<<endl;
for(const auto & employee : employees){
employee->printShort(os);
}
return os;
}
bool Manager::findEmployee(int id) {
int i = 0;
for(const auto & employee : employees){
cout<<++i<<endl;
if(employee->getId() == id){
cout<<"return true"<<endl;
return true;
}
}
cout<<"return false"<<endl;
return false;
}
bool Manager::isWorkFlag() const {
return work_flag;
}
void Manager::setWorkFlag(bool workFlag) {
work_flag = workFlag;
}
and this is the main function:
int main() {
Employee e1(1, "John", "Williams", 2002);
Employee e2(2, "Alex", "Martinez", 2000);
Manager m1(1,"Robert", "stark", 1980);
m1.addEmployee(&e1);
m1.addEmployee(&e2);
Employee e3(7, "Reuven", "Guetta", 2001);
m1.addEmployee(&e3);
m1.printLong(cout);
cout<<"Delete"<<endl;
//here is the problem
m1.removeEmployee(e2.getId());
m1.printLong(cout);
return 0;
}

shared_ptr<Employee> employee(employee_add);
There is only one reason to have a shared_ptr, in the first place; there's only one reason for its existence; it has only one mission in its life, as explained in every C++ textbook: to be able to new an object, and have the shared_ptr automatically take care of deleteing it when all references to the object are gone, avoiding a memory leak.
In your program this object was not instantiated in dynamic scope with new:
Employee e2(2, "Alex", "Martinez", 2000);
Manager m1(1,"Robert", "stark", 1980);
m1.addEmployee(&e1);
// etc, etc, etc...
and that's the reason for the crash.
If you are not using new, simply get rid of all shared_ptrs in the shown code.

Related

Member function doesn't work when using pointer to class

Scenario: I have two classes, each contains a pointer to the other (when using them, being able to refer to the other is going to be important so I deemed this appropriate). When I try accessing a private variable from one class via using the pointer to the other and a getter function inside that, it works perfectly.
Problem: Using a setter (in this case, addPoints)/manipulating the variables however leads to no result.
I'm new so anything here might be "improper etiquette" and bad practice. Feel free to point them out! But please also try to provide a solution. This is also my first question on SO, so please be gentle!
Related code pieces:
Team.h
#include "Driver.h"
using namespace std;
class Team {
int Points = 0;
vector<Driver*> Drivers;
public:
void addPoints(int gained); //does not work
int getPoints(); //works perfectly
Driver getDriver(int nr);
void setInstance(vector<Driver*> drivers);
};
Team.cpp
#include "Team.h"
#include "Driver.h"
using namespace std;
void Team::addPoints(int gained) {
this->Points = this->Points + gained;
}
int Team::getPoints() {
return this->Points;
}
Driver Team::getDriver(int nr) {
return *Drivers[nr];
}
void Team::setInstance(vector<Driver*> drivers) {
this->Drivers = drivers;
}
Driver.h
using namespace std;
class Team;
class Driver {
int Points = 0;
Team* DriversTeam;
public:
void SetTeam(Team& team);
Team getTeam();
int getPoints(); //works
void addPoints(int gained); //doesn't work
};
Driver.cpp
#include "Driver.h"
#include "Team.h"
using namespace std;
void Driver::SetTeam(::Team& team) {
this->DriversTeam = &team;
}
Team Driver::getTeam() {
return *DriversTeam;
}
int Driver::getPoints() {
return this->Points;
}
void Driver::addPoints(int gained) {
this->Points = this->Points + gained;
}
Initializer.cpp (linking drivers to teams)
void InitializeData(vector<Team>& teams, vector<Driver> &drivers) {
//(...)
//reads each team in from data file to memory
//key part:
vector<Driver*> teamsDrivers;
for (auto& iter : drivers) { //this loop mainly determines which driver to link with which teams
if (iter.getName().compare(values[4]) == 0) { //values is csv line data in a string vector. I guess not the prettiest parsing method here but will be revised
teamsDrivers.push_back(&iter);
}else if(iter.getName().compare(values[5]) == 0) {
teamsDrivers.push_back(&iter);
}
}
tempTeam.setInstance(teamsDrivers);
teams.push_back(tempTeam);
}
(linking driver to team)
//drivers are linked to teams last, as they are declared first (so I cannot link them to the yet nonexisting teams)
void LinkTeam(vector<Driver>& drivers, vector<Team>& teams) {
for (auto& driverIter : drivers) { //iterate through drivers
for (auto& teamIter : teams) { // iterate through teams
bool found = 0;
for (size_t i = 0; i < teamIter.DriverAmount(); i++) {
if (driverIter.getName() == teamIter.getDriver(i).getName()) {
driverIter.SetTeam(teamIter);
found = 1;
break;
}
}
if (found) { //exit iterating if driver is found
break;
}
}
}
}
Example of use in main.cpp
teams[0].addPoints(10);
drivers[3].getTeam().addPoints(15); //driver 3 is linked to team 0
cout << teams[0].getPoints(); //15
cout << drivers[3].getTeam().getPoints(); //15
teams[0].getDriver(1).addPoints(20); //driver 1 of team 0=driver[3]
drivers[3].addPoints(25);
cout << drivers[3].getPoints(); //25
cout << teams[0].getDriver(1).getPoints(); //25
Thanks for the help in advance.
This is quite simple:
Your getTeam() and getDriver() functions are returning copies of the objects, not references, so the addPoints() are performed on temporary copies and not the real ones.
To fix it, simply change the return types to references (add &):
Team& getTeam();
and
Driver& getDriver();

vector<string> doesnt work when using seperate classes

I have a txt file which has a rogue-like level
I load it like so:
void File::LoadLevel()
{
ifstream input_file;
input_file.open("Level_1.txt");
if (input_file.fail())
{
perror("Level_1.txt");
}
while (input_file >> _level)
{
_level_instance.push_back(_level);
}
}
variables:
string _level;
vector<string> _level_instance;
I print it out like this:
for (int i = 0; i < _level_instance.size(); i++)
{
cout << _level_instance[i] << endl;
}
which works fine.
however I have a vector in another class as well and i use a getter like this:
vector<string>GetlevelData(){ return _level_data; }
and I change the LoadLevel() from this:
_level_instance.push_back(_level);
to this:
Level Lvl;
Lvl.GetLevelData().pushback(_level);
I make an method in 'Level' class which prints it out to the screen same as before
it compiles but it doesnt print out anything why?
By the way in the int main() neccesary methods are executed which is OpenLevel() from File class and Print() from Level class
EDIT:
passing it &by reference didnt work
here are both methods in Level.h:
void SetLevelData(const std::string &string) {
_level_data.push_back(string);
}
//Getters
vector<string>& GetlevelData(){ return _level_data; }
in File.cpp:
void File::LoadLevel()
{
ifstream input_file;
Level lvl;
input_file.open("Level_1.txt");
if (input_file.fail())
{
perror("Level_1.txt");
}
while (input_file >> _level)
{
lvl.GetlevelData().push_back(_level);
}
}
it doesnt work it prints nothing, even though i added '&'
the result is same when i try it with 'push_back' method in Level.h
Here is int main() just in case:
int main()
{
File f;
Level lvl;
f.LoadLevel();
lvl.PrintLevel();
system("PAUSE");
return 0;
}
And PrintLevel() in Level.cpp:
void Level::PrintLevel()
{
for (int i = 0; i < _level_data.size(); i++)
{
cout << _level_data[i] << endl;
}
}
The GetlevelData function returns its vector by value which means a whole new copy of it is created. Anything you push into that vector will be lost when the copy goes out of scope (which happens when the expression Lvl.GetLevelData().pushback(_level) is done).
You should return it by reference instead:
vector<string>& GetlevelData(){ return _level_data; }
// ^
// |
// Return by reference
vector<string>GetlevelData() returns a copy of the vector and not a reference to it.
So your Lvl.GetLevelData().push_back(_level); returns a copy adds data to the vector and then deletes that copy again. So you add it to a vector that will be immediately be deleted again.
You would need to return it either by reference:
vector<string>& GetlevelData(){ return _level_data; }
Or write a method to push back the data:
void push_back( const std::string &string) {
_level_data.push_back(string);
}

How do I return a Null Pointer in a function C++

I am currently working on a bit of code that will search within a vector of type Person (which I have defined in the code and will show if needed). If it finds the person, it returns their name. This is currently working, but if it does not find the person, it is supposed to return a Null pointer. The problem is, I cannot figure out how to make it return a Null pointer! It just keeps either crashing the program every time.
Code:
Person* lookForName(vector<Person*> names, string input)
{
string searchName = input;
string foundName;
for (int i = 0; i < names.size(); i++) {
Person* p = names[i];
if (p->getName() == input) {
p->getName();
return p; //This works fine. No problems here
break;
} else {
//Not working Person* p = NULL; <---Here is where the error is happening
return p;
}
}
}
You could use std::find_if algorithm:
Person * lookForName(vector<Person*> &names, const std::string& input)
{
auto it = std::find_if(names.begin(), names.end(),
[&input](Person* p){ return p->getName() == input; });
return it != names.end() ? *it : nullptr; // if iterator reaches names.end(), it's not found
}
For C++03 version:
struct isSameName
{
explicit isSameName(const std::string& name)
: name_(name)
{
}
bool operator()(Person* p)
{
return p->getName() == name_;
}
std::string name_;
};
Person * lookForName(vector<Person*> &names, const std::string& input)
{
vector<Person*>::iterator it = std::find_if(names.begin(), names.end(),
isSameName(input));
return it != names.end() ? *it : NULL;
}
If the name you are searching for is not at the first element, then you are not searching in the rest of the elements.
You need to do something like -
for (int i = 0; i<names.size(); i++){
Person* p = names[i];
if (p->getName() == input) {
return p;
// Placing break statement here has no meaning as it won't be executed.
}
}
// Flow reaches here if the name is not found in the vector. So, just return NULL
return NULL;
As Chris suggested, try using std::find_if algorithm.
Looks like you just have to return Null, nullptr, or 0.
codeproject
Just use following code:
return NULL;

map/set iterator is not dereferencable C++ map

Please, have a look at my code.
int main () {
Program* allcommand = new Program;
allcommand->addCommand("add", new Add);
allcommand->addCommand("convert", new Convert);
allcommand->addCommand("exit", new Exit);
allcommand->addCommand("help", new Help);
allcommand->addCommand("show", new Show);
allcommand->addCommand("modify", new Modify);
std::string input;
Command* obj;
while (true) {
std::cout << "\nCommand >> ";
std::getline(std::cin, input);
std::map<std::string, Command*> :: iterator it;
std::vector<std::string> parsedinput = allcommand->parse(input);
it = allcommand->getCommands().find(parsedinput[0]);
obj = it->second;
obj->start(parsedinput);
delete obj;
}
return 0;
}
It registers commands to a map which holds its command name and pointer to its class.
This compiles without problems but when I enter a command, it crashes with "map/set iterator not dereferencable". I am new to maps (few minutes) so please help.
EDIT. Ok I found that the problem is not in main... Here is code of Program class (some of it)
void Program::addCommand(std::string command1, Command* obj) {
m_allCommands[command1] = obj;
}
std::map<std::string, Command*> Program::getCommands () {
return m_allCommands;
}
I think the problem is here, because after i register commands in main, I cannot cout the name of any command (same problem)
std::map<std::string, Command*> Program::getCommands () {
return m_allCommands;
}
returns a copy of the m_allcommands map. So when you do:
it = allcommand->getCommands().find(parsedinput[0]);
You get an iterator on the temporary object returned by allcommand->getCommands() that gets destroyed when the assignment is done. Therefore it points to nothing.
Change getCommands() to:
std::map<std::string, Command*>& Program::getCommands () {
return m_allCommands;
}
or even better:
const std::map<std::string, Command*>& Program::getCommands () const {
return m_allCommands;
}
After a call to find() you need to check if
if(it == allcommand->getCommands().end()) {
//Not Found
} else {
obj = it->second;
obj->start(parsedinput);
}

How to implement final conditions properly?

This is what I'm trying to do (this is a simplification of a real project):
int param;
int result;
void isolated(int p) {
param = p;
try {
// make calculations with "param" and place the
// result into "result"
process();
} catch (...) {
throw "problems..";
}
}
I can't change the way process() works, since this function is not created in the project and is a third-party function. It works with global variables param and result and we can't change this.
The problem appears when isolated() is called back from process() with another parameter. I want to catch this situation, but don't know how to do it, since finally is absent in C++. I feel that I should use RAII technique, but can't figure out how to do it in this case properly.
This is how I can make it with code duplication:
int param;
int result;
void isolated(int p) {
static bool running;
if (running) {
throw "you can't call isolated() from itself!";
}
running = true;
param = p;
try {
// make calculations with "param" and place the
// result into "result"
process();
running = false;
} catch (...) {
running = false; // duplication!
throw "problems..";
}
}
"finally" like situations are handled in C++ using guard objects, that do their finally thing in the destructor. This is IMHO much more powerful approach, because you have to analyze the situation to finalize in order to create a reuseable object. In this case, we need to make process rentrant, because parameters and returns are passed in globals. The solution is to save their values on entry and restore them on exit:
template<class T>
class restorer
{
T &var; // this is the variable we want to save/restore
T old_value; // the old value
restorer(const restorer&);
void operator=(const restorer&);
public:
restorer(T &v) : var(v), old_value(v) {}
~restorer() { var=old_value; }
};
int param;
int result;
int isolated(int p) {
restorer<int> rest_param(param);
restorer<int> rest_result(result);
param = p;
try {
// make calculations with "param" and place the
// result into "result"
process();
return result;
} catch (...) {
return 0;
}
}
Maybe I didn't get it right, but why don't you use a flag? You want to know when the isolated() is called from the process(), right?
int isolated(int p) {
static int execDeep = 0;
execDeep++;
// your code here
execDeep--;
}
Now you can check 'execDeep' value, > 1 means it is called from the process() while still being executed.
I still don't quite sure how finally is related here, but you could try Boost.ScopeExit if you want to avoid creating a scope guard structure yourself.
Example:
#include <boost/scope_exit.hpp>
#include <cstdio>
int isolated(int p) {
static bool running = false;
if (running) {
printf("Throwing %d\n", p);
throw p;
}
printf("Starting %d\n", p);
running = true;
BOOST_SCOPE_EXIT( (p)(&running) ) { // <--
printf("Stopping %d\n", p); // <--
running = false; // <--
} BOOST_SCOPE_EXIT_END // <--
// ...
if (p)
isolated(p*10);
// ...
printf("Returing %d\n", p);
return 4;
}
int main() {
printf(">> first\n");
isolated(0);
printf(">> second\n");
try {
isolated(1);
printf(">> third (should not be printed.)\n");
} catch(int p) {
printf("Caught %d\n", p);
}
isolated(0);
printf(">> fourth\n");
return 0;
}
Result:
>> first
Starting 0
Returing 0
Stopping 0
>> second
Starting 1
Throwing 10
Stopping 1
Caught 10
Starting 0
Returing 0
Stopping 0
>> fourth
Could this work?
int save = -10000000000;
int param;
int result;
int isolated(int p) {
if (save != -10000000000)
{
// run the other condition
}
else
{
save = p;
param = p;
try {
// make calculations with "param" and place the
// result into "result"
process();
return result;
} catch (...) {
return 0;
}
}
}
If I understand correctly, you want to automatically set the running flag to false at the end of function. If that is the requirement then you can use the ScopeGuard approarch mentioned in the link.