Regarding STL

This is a discussion on Regarding STL within the C++ Programming forums, part of the General Programming Boards category; Hello. I am new to STL. I have written a c++ program which compiles correctly but crash when i run ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    Regarding STL

    Hello.

    I am new to STL. I have written a c++ program which compiles correctly but crash when i run it. Please tell me what is wrong here. Thanks in advance.

    Code:
    #include <set>
    #include <map>
    
    class DataType {
    public:
          std::set<string> dataSubtypeSet;
    };
    typedef std::map<string, DataType* >    DataTypeMap;
    
    class TemplateData {
    public:
          DataTypeMap dataTypeMap;
    };
    
    int main(){
    
         TemplateData *templateData = new TemplateData();
          int sizeB = templateData->dataTypeMap.size();
          string dataTypeValue;
          DataType *dataType = new DataType();
          templateData->dataTypeMap.insert(std::make_pair(dataTypeValue, dataType));
          int sizeA = templateData->dataTypeMap.size();
    }
    Regards,
    Sunny.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Compiles correctly? Not for me:
    1>------ Build started: Project: CProg2, Configuration: Debug Win32 ------
    1>Compiling...
    1>CProg2.cpp
    1>c:\...\cprog2\cprog2.cpp(6) : error C2065: 'string' : undeclared identifier
    1>c:\...\cprog2\cprog2.cpp(17) : error C2512: 'TemplateData' : no appropriate default constructor available
    1>c:\...\cprog2\cprog2.cpp(18) : error C2662: 'std::_Tree<_Traits>::size' : cannot convert 'this' pointer from 'DataTypeMap' to 'const std::_Tree<_Traits> &'
    1> Reason: cannot convert from 'DataTypeMap' to 'const std::_Tree<_Traits>'
    1> Conversion requires a second user-defined-conversion operator or constructor
    1>c:\...\cprog2\cprog2.cpp(19) : error C2146: syntax error : missing ';' before identifier 'dataTypeValue'
    1>c:\...\cprog2\cprog2.cpp(19) : error C2065: 'dataTypeValue' : undeclared identifier
    1>c:\...\cprog2\cprog2.cpp(20) : error C2512: 'DataType' : no appropriate default constructor available
    1>c:\...\cprog2\cprog2.cpp(21) : error C2663: 'std::_Tree<_Traits>::insert' : 2 overloads have no legal conversion for 'this' pointer
    1>c:\...\cprog2\cprog2.cpp(22) : error C2662: 'std::_Tree<_Traits>::size' : cannot convert 'this' pointer from 'DataTypeMap' to 'const std::_Tree<_Traits> &'
    1> Reason: cannot convert from 'DataTypeMap' to 'const std::_Tree<_Traits>'
    1> Conversion requires a second user-defined-conversion operator or constructor
    1>Build log was saved at "file://c:\...\CProg2\Debug\BuildLog.htm"
    1>CProg2 - 8 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Putting #include <string> and std:: in front of all your instances of "string" will fix those. After that it compiles/links and runs although you should fix the memory leaks (you forgot to delete those two pointers you allocate).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Formatting Using STL
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 04:52 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 02:36 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21