Removing segmentation fault and working with destructor c++ [duplicate] - c++11

This question already has answers here:
How do I find the length of an array?
(30 answers)
What does sizeof do?
(4 answers)
Closed 10 months ago.
This is student.hpp (Base class)
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
class student
{
public:
student(const std::string& name, const std::string& ssn);
virtual ~student();
virtual void identify();
protected:
// declaring it protected so that i can use it for derived class
std::string studName;
std::string studSSN;
};
student::student(const std::string& name, const std::string& ssn)
: studName(name), studSSN(ssn)
{
std::cout << "Parent Constructor" << std::endl;
}
// destructor for the class
student::~student()
{
std::cout << "Parent Destructor" << std::endl;
}
// displays the information about the student
void student::identify()
{
std::cout << "Name: " << studName << std::endl;
std::cout << "SSN: " << studSSN << std::endl;
}
#endif
This is studentAthlete.hpp (derived class)
#ifndef STUDENTATHLETE_H
#define STUDENTATHLETE_H
#include <iostream>
#include <string>
#include "student.hpp"
class studentAthlete : public student
{
public:
studentAthlete(const std::string& name, const std::string& ssn, const std::string& game);
~studentAthlete();
void identify();
private:
std::string sports;
};
studentAthlete::studentAthlete(const std::string& name, const std::string& ssn, const std::string& game)
: student(name,ssn),sports(game)
{
std::cout << "Child Constructor" << std::endl;
}
studentAthlete::~studentAthlete()
{
std::cout << "Child Destructor" << std::endl;
}
void studentAthlete::identify()
{
student::identify();
std::cout << "Sports: " << sports << std::endl;
std::cout << "Student is an athlete." << std::endl;
}
#endif
This is the main class
#include <iostream>
#include <string>
#include "student.hpp"
#include "studentAthlete.hpp"
int main()
{
student ja("John Anderson", "345-12-3547");
student bw("Bill Williams", "286-72-6194");
studentAthlete bj("Bob Johnson", "294-87-6295", "football");
studentAthlete dr("Dick Robinson", "669-28-9296", "baseball");
// list of student pointers
student* stud[] = {&ja, &bw, &bj, &dr};
int stud_size = (int) sizeof(stud);
for (int i = 0; i < stud_size; i++)
{
stud[i]->identify();
std::cout << std::endl << std::endl;
}
return EXIT_SUCCESS;
}
I tried using the delete function for dislocating pointer or memory leaks.
I don't know how to get rid of the error or to use a destructor.
I am having segmentation fault
zsh: segmentation fault ./"main"
Any help destructor or removing segmentation fault would be appreciated.
Thank you.

Related

Begin was not declared in the scope for Generic Array Template Class

#include <iostream>
#include <string>
template <typename T, int N>
class Array {
int size{N};
T values{N};
friend std::ostream &operator<<(std::ostream &os, const Array<T, N> &arr) {
os << "[";
for (const auto &val: arr.values)
os << val << " ";
os << "]" << std::endl;
return os;
}
public:
Array() = default;
Array(T value_added) {
for(auto &value: values)
value = value_added;
}
void fill(T data_value) {
for (auto &value: values)
value = data_value;
}
int get_size() const {
return size;
}
T &operator[](int index) {
return values[index];
}
};
int main () {
Array<int,2> nums;
std::cout << "The size of nums is: " << nums.get_size() << std::endl;
std::cout << nums << std::endl;
}
I am trying to create a generic array template class but I get the error Begin was not declared in the scope and it says it is due to the for (const auto &val: arr.values) loop.

How to work out types required for std::for_each lambda function

I am confused about how to work out what type is required to use a lambda function in std::for_each. It seems I cannot use auto in this case as a parameter (Visual Studio 2013 complains anyway).
In the code below I would have thought I would use sections as the type for the for_each function, but in fact I have to use
const std::pair<std::string, std::unordered_map<std::string, std::string> >
And for the 'inner' type in this case what do I use?
My main question is how I work out what type to use?
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>
// key=value pairs within a section
typedef std::unordered_map<std::string, std::string> keyvalue;
// [section1] - top level
typedef std::unordered_map< std::string, std::unordered_map<std::string, std::string> > sections;
class config
{
public:
config() {
setup();
}
typedef sections::iterator iterator;
iterator begin() { return sections_.begin(); }
iterator end() { return sections_.end(); }
private:
sections sections_;
void setup() {
// obviously we wouldn't hard code like this in a real program
sections_["programming languages"].insert(std::make_pair("C", "imperative"));
sections_["programming languages"].insert(std::make_pair("C++", "OOP"));
sections_["programming languages"].insert(std::make_pair("Java", "OOP"));
sections_["programming languages"].insert(std::make_pair("Haskell", "functional"));
sections_["programming languages"].insert(std::make_pair("Prolog", "logic"));
}
};
int main() {
config cfg;
std::for_each(cfg.begin(), cfg.end(), [](const std::pair<std::string, std::unordered_map<std::string, std::string> > sec) {
std::cout << "section name: " << sec.first << std::endl;
// what is inner type - thought it would be keyvalue ???
//std::for_each(sec.second.begin(), sec.second.end(), [](const keyvalue& pr) {
//std::cout << "first: " << pr << std::endl;
//});
});
// I thought type would be sections ???
//std::for_each(cfg.begin(), cfg.end(), [](const sections& sec) {
// std::cout << "section name: " << sec.first << std::endl;
//});
}
You can also use decltype for this purpose like this:
config cfg;
std::for_each(cfg.begin(), cfg.end(), [](const decltype(*cfg.begin())& sec) {
std::cout << "section name: " << sec.first << std::endl;
std::for_each(sec.second.begin(), sec.second.end(), [](const decltype(*sec.second.begin())& pr) {
std::cout << "first: " << pr.first << std::endl;
});
});
I'm not 100% certain how reference types affect this, so removing it might be more appropriate like const std::remove_reference<decltype(*cfg.begin())>::type& sec
Although it should be noted that VS2013 ItelliSense has trouble with decltype and will mark it as an error even if it compiles fine.
STL containers always define a value_type so in that case you can use decltype(inst)::value_type and you don't need the remove_reference shenanigans. So I would advise you to also add a value_type typedef to your type like this:
class config
{
public:
typedef sections::value_type value_type;
Then you can do:
config cfg;
std::for_each(cfg.begin(), cfg.end(), [](const decltype(cfg)::value_type& sec) {
std::cout << "section name: " << sec.first << std::endl;
std::for_each(sec.second.begin(), sec.second.end(), [](const decltype(sec.second)::value_type& pr) {
std::cout << "first: " << pr.first << std::endl;
});
});

segfault when accessing static std::map

I'm trying to refactor a rather complicated piece of code and run into segfaults during loading the library. This is a minimal example of what I could single out to be the source of the segfault:
#include <iostream>
#include <string>
#include <map>
class Manager {
public:
class Action{
};
static bool registerAction(const Action* a, const char* name);
};
namespace Actions {
class ExampleAction : public Manager::Action {
};
namespace {
static bool available = Manager::registerAction(new ExampleAction(),"ExampleAction");
}
}
namespace {
typedef std::map<const std::string,const Manager::Action*> ActionList;
static ActionList sActions;
}
bool Manager::registerAction(const Action* a, const char* name){
std::cout << "attempting to register action " << a << " as " << name << std::endl;
sActions[name] = a;
std::cout << "done" << std::endl;
return true;
}
int main(){
std::cout << "hello!" << std::endl;
for(auto it:sActions){
std::cout << it.first << std::endl;
}
std::cout << "world!" << std::endl;
return 0;
}
It compiles fine with g++ 4.8.4 using the --std=c++11 flag, but upon execution, this happens:
attempting to register action 0x1ebe010 as ExampleAction
Segmentation fault (core dumped)
The line attempting to register comes first, which is of course expected, but the line assigning the value to the static map instance causes the crash, and I don't understand the reason. I'm probably being stupid here, but still - any suggestions on how to fix this?

Different behavior with similar code

#include <vector>
#include <iostream>
using namespace std;
struct A
{
vector<int> v;
};
void f0(const A&& a0)
{
cout << &a0.v[0] << endl;
A a1{ move(a0.v) };
cout << &a1.v[0] << endl << endl;;
}
void f1()
{
A a0{ vector<int>(10) };
cout << &a0.v[0] << endl;
A a1{ move(a0.v) };
cout << &a1.v[0] << endl;
}
int main()
{
f0(A{ vector<int>(10) });
f1();
return 0;
}
I can't understand why in the first case addresses are different but in the second case addresses are same.

Error: Identifier "PrintGuessesRemaining" , "PrintWordSpaceDelinated" , "PrintWordsRemainingIsUndefined"

I have Created a Print Class where I have created all of those functions.
When I attempt to call these functions, I get the Identifier Error. I am sure I have incorrectly set up my class. Please Help construct Print Class
Code attached:
// Print.cpp - Print Class implementation
// Written by Varun Patel
#include "Print.h"
Print::Print()
{
}
Print::Print(const string& word)
{
Word = word;
}
void Print::PrintWordsRemaining(set<string>& possibleWords_, bool displayNumberOfWordsRemaining_)
{
if(displayNumberOfWordsRemaining_)
{
cout << "There are " << possibleWords_.size() << " possible words left." << endl;
}
else
{
//Do nothing
}
}
void Print::PrintWordSpaceDelinated(string word_)
{
for (size_t i = 0; i < word_.size(); i++)
{
cout << word_[i] << " ";
}
cout << endl;
}
void Print::PrintGuessesRemaining(int guessesRemaining_)
{
cout << "You have " << guessesRemaining_ << " guesses remaining." << endl;
}
Here is how i try to call one of the functions:
#include "UpdateGuessesRemaining.h"
void UpdateGuessesRemaining(set<string>& newPossibleWords, int& guessesRemaining,
char guessChar, string& guessedWord)
{
set<string>::iterator wordPtr = newPossibleWords.begin();
if (wordPtr->find(guessChar) == -1)
{
cout << "Sorry, incorrect guess. ";
PrintGuessesRemaining(--guessesRemaining);
}
else
{
cout << "Correct! The word contains " << guessChar << "." << endl;
}
}
Here is my header File:
// Print.h - Print Class declaration
// Written by Varun Patel
#pragma once
#include <iostream>
#include <set>
#include <string>
#include "PromptForGuessesRemaining.h"
using namespace std;
class Print
{
public:
// Default constructor
Print();
Print(const string& word);
void PrintWordsRemaining(set<string>& possibleWords, bool displayNumberOfWordsRemaining);
void PrintWordSpaceDelinated(string word);
void PrintGuessesRemaining(int guessesRemaining);
private:
string Word;
};
Thanks For Your Help,
Varun

Resources