Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include <unordered_map>
  2. #include <vector>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. // payload class, which is stored in the container class (see below)
  9. class TestFunction {
  10. public:
  11. void setTestFunction(vector<double> func) {
  12. function = func;
  13. }
  14. void resize(vector<double> func) {
  15. function.resize(func.size());
  16. }
  17. private:
  18. vector<double> function;
  19. };
  20.  
  21. // main class, which has an unordered map as member. I want to store objects of the second class (see above) in it
  22. class TestContainer {
  23. public:
  24. void setContainer(int index, TestFunction function) {
  25. cout << "Trying to fill container" << endl;
  26. m_container[index]=function; // <---------------- This line causes a segfault, if the member function is used on a pointer
  27. cout << "Done!" << endl;
  28. }
  29. private:
  30. unordered_map<int,TestFunction> m_container;
  31. };
  32.  
  33. #include <TestClass.h>
  34.  
  35. int main(void) {
  36. //define two objects, one is of type TestContainer, the other one is a pointer to a TestContainer
  37. TestContainer testcontainer1, *testcontainer2;
  38.  
  39. // initialize a test function for use as payload
  40. TestFunction testfunction;
  41. vector<double> testvector = {0.1,0.2,0.3};
  42.  
  43. // prepare the payload object
  44. cout << "Setting test function" << endl;
  45. testfunction.resize(testvector);
  46. testfunction.setTestFunction(testvector);
  47.  
  48. // fill the payload into testcontainer1, which works fine
  49. cout << "Filling test container 1 (normal)" << endl;
  50. testcontainer1.setContainer(1,testfunction);
  51.  
  52. // fill the same payload into testcontainer2 (the pointer), which gives a segfault
  53. cout << "Filling test container 2 (pointer)" << endl;
  54. testcontainer2->setContainer(1,testfunction);
  55.  
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement