Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualbedtime

Posted 27 November 2012 - 08:56 PM

Edit,


still not working... sigh




I've been at this for a day now and no progress. I read the article and I've still not got this other part of the code to work. I appear to have an issue with my Map object not being being recognised even though i've declared it and its within the same class. I've tried the map object as a pointer as suggested in the link (its not a pointer because i dont feel like changing all the other stuff around by adding the arrow insertion operator for the 5th time.... Pointer or not both result in seg faults. Out of everything in c++ these cyclic dependancies are the holding my code back the most. This is really quite frustrating. The article said in 99% of cases adding a forward declaration and pointer to object of another class is enough. I'm missing something.

Here is the code:

[source lang="cpp"]// game.h#ifndef GAME_H#define GAME_H#include <map>#include <vector>#include "staticcount.h"#include "value.h"#include "tvalue.h"class GameObject;typedef std::pair<int, int> Key;class Game{ Value value; std::multimap<std::pair<int, int>, GameObject*> Map; std::multimap<std::pair<int, int>, GameObject*> go2; // x chunk, y chunk, gameObject std::multimap<int, std::multimap<int, GameObject*>*> go;public: Game() { value.newNum("game number", StaticCount::getNumGames()->getNum()); } // get game values Value* val() { return &value; } void loop(int x_begin, int x_end, int y_begin, int y_end); std::multimap<std::pair<int, int>, GameObject*> getMap();};#endif[/source]


[source lang="cpp"]// game.cpp#include <iostream>#include "game.h"#include "gameobject.h"#include "keys.h"std::multimap<std::pair<int, int>, GameObject*> Game::getMap(){ return Map;}void Game::loop(int x_begin, int x_end, int y_begin, int y_end){ /* uncommenting the line below makes the code below works but its useless as it goes out of scope and is not stored. if the line is commented i get a seg fault */ //std::multimap<std::pair<int, int>, GameObject*> Map; GameObject* gameObj1 = new GameObject; gameObj1->val()->setNum("test", 10); // seg fault is from the line below: (this works if above code 6 lines above is uncommented) Map.insert({std::make_pair<int, int>(1, 2), gameObj1}); std::cout << "made this far" << std::endl; GameObject* gameObj2 = new GameObject; gameObj2->val()->setNum("test", 20); Map.insert({std::make_pair<int, int>(3, 4), gameObj2}); GameObject* gameObj3 = new GameObject; gameObj3->val()->setNum("test", 30); Map.insert({std::make_pair<int, int>(5, 10), gameObj3}); x_begin = 2; y_begin = 1; x_end = 5; y_end = 9; x_end++; y_end++; // if (x_end <= x_begin) return; // if (y_end <= y_begin) return; Key lower(x_begin, y_begin); Key upper(x_end - 1, y_end); auto first = Map.lower_bound(lower); auto last = Map.lower_bound(upper); for (auto itr = first; itr != last;) { int x = itr->first.first; int y = itr->first.second; if (y < y_begin) { itr = Map.lower_bound(Key(x, y_begin)); } else if (y >= y_end) { itr = Map.lower_bound(Key(x + 1, y_begin)); } else { std::cout << "x: " << x << " y: " << y <<" test: " << itr->second->val()->getNum("test") << std::endl; ++itr; } }}[/source]

#5bedtime

Posted 27 November 2012 - 08:46 PM

*** EDIT ***

I think i've solved it. it was something else. i think declaring the NEW the incorrect way.



SOLVED




I've been at this for a day now and no progress. I read the article and I've still not got this other part of the code to work. I appear to have an issue with my Map object not being being recognised even though i've declared it and its within the same class. I've tried the map object as a pointer as suggested in the link (its not a pointer because i dont feel like changing all the other stuff around by adding the arrow insertion operator for the 5th time.... Pointer or not both result in seg faults. Out of everything in c++ these cyclic dependancies are the holding my code back the most. This is really quite frustrating. The article said in 99% of cases adding a forward declaration and pointer to object of another class is enough. I'm missing something.

Here is the code:

[source lang="cpp"]// game.h#ifndef GAME_H#define GAME_H#include <map>#include <vector>#include "staticcount.h"#include "value.h"#include "tvalue.h"class GameObject;typedef std::pair<int, int> Key;class Game{ Value value; std::multimap<std::pair<int, int>, GameObject*> Map; std::multimap<std::pair<int, int>, GameObject*> go2; // x chunk, y chunk, gameObject std::multimap<int, std::multimap<int, GameObject*>*> go;public: Game() { value.newNum("game number", StaticCount::getNumGames()->getNum()); } // get game values Value* val() { return &value; } void loop(int x_begin, int x_end, int y_begin, int y_end); std::multimap<std::pair<int, int>, GameObject*> getMap();};#endif[/source]


[source lang="cpp"]// game.cpp#include <iostream>#include "game.h"#include "gameobject.h"#include "keys.h"std::multimap<std::pair<int, int>, GameObject*> Game::getMap(){ return Map;}void Game::loop(int x_begin, int x_end, int y_begin, int y_end){ /* uncommenting the line below makes the code below works but its useless as it goes out of scope and is not stored. if the line is commented i get a seg fault */ //std::multimap<std::pair<int, int>, GameObject*> Map; GameObject* gameObj1 = new GameObject; gameObj1->val()->setNum("test", 10); // seg fault is from the line below: (this works if above code 6 lines above is uncommented) Map.insert({std::make_pair<int, int>(1, 2), gameObj1}); std::cout << "made this far" << std::endl; GameObject* gameObj2 = new GameObject; gameObj2->val()->setNum("test", 20); Map.insert({std::make_pair<int, int>(3, 4), gameObj2}); GameObject* gameObj3 = new GameObject; gameObj3->val()->setNum("test", 30); Map.insert({std::make_pair<int, int>(5, 10), gameObj3}); x_begin = 2; y_begin = 1; x_end = 5; y_end = 9; x_end++; y_end++; // if (x_end <= x_begin) return; // if (y_end <= y_begin) return; Key lower(x_begin, y_begin); Key upper(x_end - 1, y_end); auto first = Map.lower_bound(lower); auto last = Map.lower_bound(upper); for (auto itr = first; itr != last;) { int x = itr->first.first; int y = itr->first.second; if (y < y_begin) { itr = Map.lower_bound(Key(x, y_begin)); } else if (y >= y_end) { itr = Map.lower_bound(Key(x + 1, y_begin)); } else { std::cout << "x: " << x << " y: " << y <<" test: " << itr->second->val()->getNum("test") << std::endl; ++itr; } }}[/source]

#4bedtime

Posted 27 November 2012 - 08:37 PM

I've been at this for a day now and no progress. I read the article and I've still not got this other part of the code to work. I appear to have an issue with my Map object not being being recognised even though i've declared it and its within the same class. I've tried the map object as a pointer as suggested in the link (its not a pointer because i dont feel like changing all the other stuff around by adding the arrow insertion operator for the 5th time.... Pointer or not both result in seg faults. Out of everything in c++ these cyclic dependancies are the holding my code back the most. This is really quite frustrating. The article said in 99% of cases adding a forward declaration and pointer to object of another class is enough. I'm missing something.

Here is the code:

[source lang="cpp"]// game.h#ifndef GAME_H#define GAME_H#include <map>#include <vector>#include "staticcount.h"#include "value.h"#include "tvalue.h"class GameObject;typedef std::pair<int, int> Key;class Game{ Value value; std::multimap<std::pair<int, int>, GameObject*> Map; std::multimap<std::pair<int, int>, GameObject*> go2; // x chunk, y chunk, gameObject std::multimap<int, std::multimap<int, GameObject*>*> go;public: Game() { value.newNum("game number", StaticCount::getNumGames()->getNum()); } // get game values Value* val() { return &value; } void loop(int x_begin, int x_end, int y_begin, int y_end); std::multimap<std::pair<int, int>, GameObject*> getMap();};#endif[/source]


[source lang="cpp"]// game.cpp#include <iostream>#include "game.h"#include "gameobject.h"#include "keys.h"std::multimap<std::pair<int, int>, GameObject*> Game::getMap(){ return Map;}void Game::loop(int x_begin, int x_end, int y_begin, int y_end){ /* uncommenting the line below makes the code below works but its useless as it goes out of scope and is not stored. if the line is commented i get a seg fault */ //std::multimap<std::pair<int, int>, GameObject*> Map; GameObject* gameObj1 = new GameObject; gameObj1->val()->setNum("test", 10); // seg fault is from the line below: (this works if above code 6 lines above is uncommented) Map.insert({std::make_pair<int, int>(1, 2), gameObj1}); std::cout << "made this far" << std::endl; GameObject* gameObj2 = new GameObject; gameObj2->val()->setNum("test", 20); Map.insert({std::make_pair<int, int>(3, 4), gameObj2}); GameObject* gameObj3 = new GameObject; gameObj3->val()->setNum("test", 30); Map.insert({std::make_pair<int, int>(5, 10), gameObj3}); x_begin = 2; y_begin = 1; x_end = 5; y_end = 9; x_end++; y_end++; // if (x_end <= x_begin) return; // if (y_end <= y_begin) return; Key lower(x_begin, y_begin); Key upper(x_end - 1, y_end); auto first = Map.lower_bound(lower); auto last = Map.lower_bound(upper); for (auto itr = first; itr != last;) { int x = itr->first.first; int y = itr->first.second; if (y < y_begin) { itr = Map.lower_bound(Key(x, y_begin)); } else if (y >= y_end) { itr = Map.lower_bound(Key(x + 1, y_begin)); } else { std::cout << "x: " << x << " y: " << y <<" test: " << itr->second->val()->getNum("test") << std::endl; ++itr; } }}[/source]

#3bedtime

Posted 27 November 2012 - 08:36 PM

I've been at this for a day now and no progress. I read the article and I've still not got this other part of the code to work. I appear to have an issue with my Map object not being being recognised even though i've declared it and its within the same class. I've tried the map object as a pointer as suggested in the link (its not a pointer because i dont feel like changing all the other stuff around by adding the arrow insertion operator for the 5th time.... Pointer or not both result in seg faults. Out of everything in c++ these cyclic dependancies are the holding my code back the most. This is really quite frustrating. The article said in 99% of cases adding a forward declaration and pointer to object of another class is enough. I'm missing something.

Here is the code:

[source lang="cpp"]// game.h#ifndef GAME_H#define GAME_H#include <map>#include <vector>#include "staticcount.h"#include "value.h"#include "tvalue.h"class GameObject;typedef std::pair<int, int> Key;class Game{ Value value; std::multimap<std::pair<int, int>, GameObject*> Map; std::multimap<std::pair<int, int>, GameObject*> go2; // x chunk, y chunk, gameObject std::multimap<int, std::multimap<int, GameObject*>*> go;public: Game() { value.newNum("game number", StaticCount::getNumGames()->getNum()); } // get game values Value* val() { return &value; } void loop(int x_begin, int x_end, int y_begin, int y_end); std::multimap<std::pair<int, int>, GameObject*> getMap();};#endif[/source]


[source lang="cpp"]// game.cpp#include <iostream>#include "game.h"#include "gameobject.h"#include "keys.h"std::multimap<std::pair<int, int>, GameObject*> Game::getMap(){ /* uncommenting results in: error: ‘Map’ was not declared in this scope */ return &Map;}void Game::loop(int x_begin, int x_end, int y_begin, int y_end){ /* uncommenting the line below makes the code below works but its useless as it goes out of scope and is not stored. if the line is commented i get a seg fault */ //std::multimap<std::pair<int, int>, GameObject*> Map; GameObject* gameObj1 = new GameObject; gameObj1->val()->setNum("test", 10); // seg fault is from the line below: (this works if above code 6 lines above is uncommented) Map.insert({std::make_pair<int, int>(1, 2), gameObj1}); std::cout << "made this far" << std::endl; GameObject* gameObj2 = new GameObject; gameObj2->val()->setNum("test", 20); Map.insert({std::make_pair<int, int>(3, 4), gameObj2}); GameObject* gameObj3 = new GameObject; gameObj3->val()->setNum("test", 30); Map.insert({std::make_pair<int, int>(5, 10), gameObj3}); x_begin = 2; y_begin = 1; x_end = 5; y_end = 9; x_end++; y_end++; // if (x_end <= x_begin) return; // if (y_end <= y_begin) return; Key lower(x_begin, y_begin); Key upper(x_end - 1, y_end); auto first = Map.lower_bound(lower); auto last = Map.lower_bound(upper); for (auto itr = first; itr != last;) { int x = itr->first.first; int y = itr->first.second; if (y < y_begin) { itr = Map.lower_bound(Key(x, y_begin)); } else if (y >= y_end) { itr = Map.lower_bound(Key(x + 1, y_begin)); } else { std::cout << "x: " << x << " y: " << y <<" test: " << itr->second->val()->getNum("test") << std::endl; ++itr; } }}[/source]

#2bedtime

Posted 27 November 2012 - 08:32 PM

I've been at this for a day now and no progress. I read the article and I've still not got this other part of the code to work. I appear to have an issue with my Map object not being being recognised even though i've declared it and its within the same class. I've tried the map object as a pointer as suggested in the link (its not a pointer because i dont feel like changing all the other stuff around by adding the arrow insertion operator for the 5th time.... Pointer or not both result in seg faults. Out of everything in c++ these cyclic dependancies are the holding my code back the most. This is really quite frustrating. The article said in 99% of cases adding a forward declaration and pointer to object of another class is enough. I'm missing something.

Here is the code:

[source lang="cpp"]// game.h#ifndef GAME_H#define GAME_H#include <map>#include <vector>#include "staticcount.h"#include "value.h"#include "tvalue.h"class GameObject;typedef std::pair<int, int> Key;class Game{ Value value; std::multimap<std::pair<int, int>, GameObject*> Map; std::multimap<std::pair<int, int>, GameObject*> go2; // x chunk, y chunk, gameObject std::multimap<int, std::multimap<int, GameObject*>*> go;public: Game() { value.newNum("game number", StaticCount::getNumGames()->getNum()); } // get game values Value* val() { return &value; } void loop(int x_begin, int x_end, int y_begin, int y_end); std::multimap<std::pair<int, int>, GameObject*> getMap();};#endif[/source]


[source lang="cpp"]// game.cpp#include <iostream>#include "game.h"#include "gameobject.h"#include "keys.h"std::multimap<std::pair<int, int>, GameObject*> getMap(){ /* uncommenting results in: error: ‘Map’ was not declared in this scope */ //return &Map;}void Game::loop(int x_begin, int x_end, int y_begin, int y_end){ /* uncommenting the line below makes the code below works but its useless as it goes out of scope and is not stored. if the line is commented i get a seg fault */ //std::multimap<std::pair<int, int>, GameObject*> Map; GameObject* gameObj1 = new GameObject; gameObj1->val()->setNum("test", 10); // seg fault is from the line below: (this works if above code 6 lines above is uncommented) Map.insert({std::make_pair<int, int>(1, 2), gameObj1}); std::cout << "made this far" << std::endl; GameObject* gameObj2 = new GameObject; gameObj2->val()->setNum("test", 20); Map.insert({std::make_pair<int, int>(3, 4), gameObj2}); GameObject* gameObj3 = new GameObject; gameObj3->val()->setNum("test", 30); Map.insert({std::make_pair<int, int>(5, 10), gameObj3}); x_begin = 2; y_begin = 1; x_end = 5; y_end = 9; x_end++; y_end++; // if (x_end <= x_begin) return; // if (y_end <= y_begin) return; Key lower(x_begin, y_begin); Key upper(x_end - 1, y_end); auto first = Map.lower_bound(lower); auto last = Map.lower_bound(upper); for (auto itr = first; itr != last;) { int x = itr->first.first; int y = itr->first.second; if (y < y_begin) { itr = Map.lower_bound(Key(x, y_begin)); } else if (y >= y_end) { itr = Map.lower_bound(Key(x + 1, y_begin)); } else { std::cout << "x: " << x << " y: " << y <<" test: " << itr->second->val()->getNum("test") << std::endl; ++itr; } }}[/source]

#1bedtime

Posted 27 November 2012 - 08:29 PM

I've been at this for a day now and no progress. I read the article and I've still not got this other part of the code to work. I appear to have an issue with my Map object not being being recognised even though it declared and within the same class. I've tried the map object as a pointer and not as a pointer as suggested in the link. Both result in seg faults. Out of everything in c++ these cyclic dependancies are the holding my code back the most. This is really quite frustrating. The article said in 99% of cases adding a forward declaration and pointer to object of another class is enough.

Here is the code:

[source lang="cpp"]// game.h#ifndef GAME_H#define GAME_H#include <map>#include <vector>#include "staticcount.h"#include "value.h"#include "tvalue.h"class GameObject;typedef std::pair<int, int> Key;class Game{ Value value; std::multimap<std::pair<int, int>, GameObject*> Map; std::multimap<std::pair<int, int>, GameObject*> go2; // x chunk, y chunk, gameObject std::multimap<int, std::multimap<int, GameObject*>*> go;public: Game() { value.newNum("game number", StaticCount::getNumGames()->getNum()); } // get game values Value* val() { return &value; } void loop(int x_begin, int x_end, int y_begin, int y_end); std::multimap<std::pair<int, int>, GameObject*> getMap();};#endif[/source]


[source lang="cpp"]// game.cpp#include <iostream>#include "game.h"#include "gameobject.h"#include "keys.h"std::multimap<std::pair<int, int>, GameObject*> getMap(){ /* uncommenting results in: error: ‘Map’ was not declared in this scope */ //return &Map;}void Game::loop(int x_begin, int x_end, int y_begin, int y_end){ /* uncommenting the line below makes the code below works but its useless as it goes out of scope and is not stored. if the line is commented i get a seg fault */ //std::multimap<std::pair<int, int>, GameObject*> Map; GameObject* gameObj1 = new GameObject; gameObj1->val()->setNum("test", 10); // seg fault is from the line below: (this works if above code 6 lines above is uncommented) Map.insert({std::make_pair<int, int>(1, 2), gameObj1}); std::cout << "made this far" << std::endl; GameObject* gameObj2 = new GameObject; gameObj2->val()->setNum("test", 20); Map.insert({std::make_pair<int, int>(3, 4), gameObj2}); GameObject* gameObj3 = new GameObject; gameObj3->val()->setNum("test", 30); Map.insert({std::make_pair<int, int>(5, 10), gameObj3}); x_begin = 2; y_begin = 1; x_end = 5; y_end = 9; x_end++; y_end++; // if (x_end <= x_begin) return; // if (y_end <= y_begin) return; Key lower(x_begin, y_begin); Key upper(x_end - 1, y_end); auto first = Map.lower_bound(lower); auto last = Map.lower_bound(upper); for (auto itr = first; itr != last;) { int x = itr->first.first; int y = itr->first.second; if (y < y_begin) { itr = Map.lower_bound(Key(x, y_begin)); } else if (y >= y_end) { itr = Map.lower_bound(Key(x + 1, y_begin)); } else { std::cout << "x: " << x << " y: " << y <<" test: " << itr->second->val()->getNum("test") << std::endl; ++itr; } }}[/source]

PARTNERS