views:

38

answers:

1
void Manager::Simulate(Military* military, Shalishut* shalishut,char* args[]){
    Simulation* simulation = Simulation::GetInstance();
    Time* time = Time::GetInstance();
    multimap<int,Task*>::const_iterator itTasks;
    itTasks = simulation->GetTasks().begin();
    while(itTasks != simulation->GetTasks().end()){
        while (itTasks->second->GetTimeStamp() == time->GetTime()){
            //TaskExecute(itTasks->second,military,shalishut,args);
            itTasks++;
        }
        // Unit take car of vehicles
        time->TimeIncrease();
    }
}

in Debug is notice that the project falls when it comes to the first while.

thanks for help.

+1  A: 

Does GetTasks() create a new map/set when it is called, and returns that? Or does it return a copy of a set where a reference would be appropriate?

If this is the case, then each call to GetTasks() returns a new object that is independent of previously returned objects. Comparing an iterator of one of these objects with an iterator of a different such object (like the begin() and end() iterators) doesn't make sense.

Make sure that all your iterators are coming from the same object and not from different copies of the same data.


Probably you want the GetTasks() function to return a reference, or a const reference, depending on constness of the Simulation object:

class Simulation {
   ...
   multimap<int,Task*>& GetTasks() { return m_tasks; } 
   const multimap<int,Task*>& GetTasks() const { return m_tasks; } 
}
sth
GetTasks() only returns a map, not creating it.
class Simulation{private: Simulation(); Simulation(const Simulation ~Simulation(); multimap<int,Task*> m_tasks; static Simulation* m_simulation;public: static Simulation* GetInstance(); multimap<int,Task*> GetTasks() const { return m_tasks; } int GetNumOfTasks() const { return m_tasks.size(); } bool AddTask(Task* task); void ShowTasks() const;};
multimap<int,Task*> GetTasks() - is returning a copy of the map
Greg Domjan
The error is "Debug assertion failed"..
how do i return a pointer to multimap<int,Task*> and not the copy ?
multimap<int, Task*> * GetTasks() const { return }
dublev
i wrote the Task class like this:
#ifndef __SIMULATION_H_#define __SIMULATION_H_ #include <map>#include <iostream>#include "Task.h"using namespace std;class Simulation{private: Simulation(); Simulation(const Simulation ~Simulation(); multimap<int,Task*> m_tasks; static Simulation* m_simulation;public: static Simulation* GetInstance(); multimap<int,Task*>* GetTasks()const { return } int GetNumOfTasks() const { return m_tasks.size(); } bool AddTask(Task* task); void ShowTasks() const;};
and now it gives me error in compilation:Error 1 error C2440: 'return' : cannot convert from 'const std::multimap<_Kty,_Ty> *' to 'std::multimap<_Kty,_Ty> *' h:\workspace\hw5\hw5\simulation.h 23
i removed the "const" from the getter, and now i get error on 1. itTasks = simulation->GetTasks().begin();2. while(simulation->GetTasks().end()){..... }Error 12 error C2228: left of '.end' must have class/struct/union h:\workspace\hw5\hw5\manager.cpp 29Error 11 error C2228: left of '.begin' must have class/struct/union h:\workspace\hw5\hw5\manager.cpp 28Error 13 error C1903: unable to recover from previous error(s); stopping compilation h:\workspace\hw5\hw5\manager.cpp 29
sorry...while(itTasks != simulation->GetTasks().end()){...}
@unser454558: I added a sample implementation of a `GetTasks` getter method that returns a reference to the map in the class.
sth
thanks it works...