-1

I'm need a static member from the type multimap

I checked that static members must be initialized (or defined) after the class declaration

The problem is that I'm not finding the correct sintax to initialize (define) the multimap that I declared

Here is my multimap declaration:

 namespace sctg
 {
        class Buffer : public BufferInterface
        {
           public:
                  ...
           private:

                  static std::multimap<std::string, std::pair<sc_core::sc_time, sc_core::sc_time> >    timeStampPackets; 
       };
 }

I'm using C++98.

vvill
  • 1,100
  • 2
  • 12
  • 21
  • What is this even supposed to do? You're trying to call a member function on an uninitialized object and then initialize that object from the return value which is an iterator not a map. And you don't use the `static` keyword to define a static member. And you don't insert into a multimap with two values. – Jonathan Wakely Jul 06 '13 at 00:27

1 Answers1

1

If all you want to do is define it, not add any members to it, then you just say:

std::multimap<std::string, std::pair<sc_core::sc_time, sc_core::sc_time> > Buffer::timeStampPackets;

outside the class definition, in the .cpp file for the class. That's it!

But life will be simpler if you use a typedef for the map type:

namespace sctg
{
  class Buffer : public BufferInterface
  {
  public:
    //  ...
  private:
    typedef std::multimap<std::string, std::pair<sc_core::sc_time, sc_core::sc_time> > TimeStampMap;

    static TimeStampMap  timeStampPackets;   // declare
  };
}

In .cpp file:

namespace sctg
{
  Buffer::TimeStampMap Buffer::timeStampPackets;  // define
}

If you want to insert a member into the map ...

If you're using C++11 you can initialize the member like this:

TimeStampMap Buffer::timeStampPackets{ { {}, { sc_core::sc_time_stamp(), sc_core::sc_time_stamp() } } };

If you can't use C++11 then the best alternative is:

TimeStampMap Buffer::timeStampPackets = getTimeStampPackets();

Where that function returns the map containing the data you want:

TimeStampMap getTimeStampPackets()
{
  TimeStampMap result;
  result.insert( TimeStampMap::value_type("", std::pair<sc_core::sc_time, sc_core::sc_time>()) );
  return result;
}
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • I've added a c++98 option, but I'm only guessing what you're trying to do, as your question is very unclear – Jonathan Wakely Jul 06 '13 at 10:58
  • I'll only define. So I use the following sintax to declare: `static std::multimap > timeStampPackets;` and the following outside of the class declaration but inside of namespace: `std::multimap > Buffer::timeStampPackets;`. But I get this error: "multiple definition of `sctg::Buffer::timeStampPackets" – vvill Jul 06 '13 at 15:51
  • You'll get that if you put the definition in the header, it must be defined only once in the program, so must be in a `.cpp` file which is compiled once – Jonathan Wakely Jul 07 '13 at 17:30