Frage

Es ist mein erstes Mal aus C ++ STL versuchen. Ich versuche, ein mehrdimensionale assoziatives Array mit Karte zu bauen. Zum Beispiel:

typedef struct DA {
    string  read_mode;
    string  data_type;
    void    *pValue;
    void    *pVarMemLoc;
}DA;

int main()
{
    map<string, map<string, map<string, map<string, map<string, DA*>>>>> DATA;

    DATA["lvl1"]["stg1"]["flr1"]["dep1"]["rom1"] = new DA;
    DATA["lvl1"]["stg1"]["flr1"]["dep1"]["rom2"] = new DA;
    DATA["lvl1"]["stg1"]["flr1"]["dep1"]["rom3"] = new DA;

    IEC["lvl1"]["stg1"]["flr1"]["dep1"]["rom1"]->read_mode = "file";
    IEC["lvl1"]["stg1"]["flr1"]["dep1"]["rom2"]->read_mode = "poll";
    IEC["lvl1"]["stg1"]["flr1"]["dep1"]["rom3"]->read_mode = "report";

    return 0;
}

Wenn oben in VS2005 den Code kompilieren, habe ich 170 von C4503 Warnungen. Alle Warnungen sind über „ergänzten Name Länge überschritten, Name abgeschnitten wurde“. Das Programm scheint jedoch in Ordnung zu laufen.

Jeder Sorgfalt einige Zeit zu ersparen, mir zu erklären, was diese Warnungen verursacht und wie ich em lösen? Vielen Dank im Voraus:)

Warning 1   warning C4503: 'std::map<_Kty,_Ty>::~map' : decorated name length exceeded, name was truncated  c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 2   warning C4503: 'std::map<_Kty,_Ty>::map' : decorated name length exceeded, name was truncated   c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 3   warning C4503: 'std::map<_Kty,_Ty>::operator []' : decorated name length exceeded, name was truncated   c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 4   warning C4503: 'std::_Tree<_Traits>::~_Tree' : decorated name length exceeded, name was truncated   c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 5   warning C4503: 'std::map<_Kty,_Ty>::operator []' : decorated name length exceeded, name was truncated   c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 6   warning C4503: 'std::_Tree<_Traits>::iterator::~iterator' : decorated name length exceeded, name was truncated  c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 7   warning C4503: 'std::_Tree<_Traits>::iterator::iterator' : decorated name length exceeded, name was truncated   c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
War es hilfreich?

Lösung

Wenn Sie diese Monster einer Datenstruktur zu halten beabsichtigen, gibt es wenig können Sie über die Warnung tun außer deaktivieren:

#pragma warning(disable:4503)

Andere Tipps

I'm not a fan of disabling the warning, because per my research, there could be unintended consequences resulting from this warning, so I prefer to truly dispense of the issue.

Here's how I would rewrite the code:

typedef struct DA {
    string  read_mode;
    string  data_type;
    void    *pValue;
    void    *pVarMemLoc;
}DA;
struct ROOM{
    map<string, DA*> map;
};
struct DEPARTMENT{
    map<string, ROOM> map;
};
struct FLOOR{
    map<string, DEPARTMENT> map;
};
struct STAGE{
    map<string, FLOOR> map;
};   
struct LEVEL{
    map<string, STAGE> map;
};   

and you could use it like this:

int main()
{
    LEVEL DATA;

    DATA.map["lvl1"].map["stg1"].map["flr1"].map["dep1"].map["rom1"] = new DA;
    DATA.map["lvl1"].map["stg1"].map["flr1"].map["dep1"].map["rom2"] = new DA;
    DATA.map["lvl1"].map["stg1"].map["flr1"].map["dep1"].map["rom3"] = new DA;

    ...
    etc

My concerns and ultimate solution primarily derive from MSDN.

Others have suggested how you could disable the warning. I suggest that you rethink your design instead. Use some more abstraction than map^5. Or change the data structure of the storage. E.g. use map instead of map^5.

Updated:

What I mean is that you basically have two options:

  • You use a key with as many strings/levels as you need:

    struct Key3 { std::string x, y, z; }; typedef std::map<Key3, DA*> MyMap;

  • Or you build something generic, where each level can hold either the DA* value and/or another level.

Declare in such way( pay attention to finished quotes)

map<string, map<string, map<string, map<string, map<string, DA*> > > > > DATA;

C++ recognizes >> as shift operator.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top