STL Map Problem with user defined Class

This is a discussion on STL Map Problem with user defined Class within the C++ Programming forums, part of the General Programming Boards category; I've a Class called Var which handles multiple basic datatypes like int, float, char, string etc.. that Class works file ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    219

    STL Map Problem with user defined Class

    I've a Class called Var which handles multiple basic datatypes like int, float, char, string etc..
    that Class works file when I use it like this.
    Code:
    Var x = (Var)2;
    cout<<x;
    But When I made a map of string and var its going Crazy.
    Code:
    typedef vector<string> 			stringList;
    typedef map<string, Var> 		paramsMap;
    
    int main(int argc, char *argv[]){
    	paramsMap lst;
    	lst["x"] = (Var)2;
    	lst["y"] = (Var)5;
    	
    	for(paramsMap::iterator it=lst.begin();it != lst.end();it++){
    		cout<<it->first<<" => "<<it->second<<endl;
    	}
    
      return EXIT_SUCCESS;
    }
    The Above Should Output.
    Code:
    x => 2
    x => 5
    But whet its showing is.
    Code:
    x => y
    y => 5
    Here is my code for var.h and var.cpp
    var.h
    http://pastebin.com/m576e6998
    var.cpp
    http://pastebin.com/m57e02d88
    toString()
    http://pastebin.com/m788b6711
    Last edited by noobcpp; 07-21-2008 at 09:51 AM.

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    I'm taking a guess but your errors come from the fact you are building a (string) temporary in your constructors and then taking the pointer value of c_str() without making a copy of it (you are just storing the address). Once the temporary is destroyed, you find yourself with a dangling pointer.

    That makes me say, you should use a std::string instead of a const char* to hold the "data". And don't forget to create a constructor for every built-in type or you'll have errors. Because this

    Code:
    const short s = 0;
    Var x(s);
    should yield an error.
    I hate real numbers.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    I understood the problem that you have focused.
    But Whats the solution if I store __data in char*
    Last edited by noobcpp; 07-21-2008 at 10:19 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    The problem is that you are assigning a pointer. There is one copy of the data in existance, which is deallocated when the std::string destructor is called.
    What do you do? You make a copy. I'll leave it to you to figure out what you have to do to make that work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    so you mean something like this Right ??
    Code:
    char* __tmp = ::toString(val).c_str();
    __data = __tmp;

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    No.
    You are working with pointers and reassigning them. Perhaps a lesson on pointers will help?

    Tutorial:
    Name: "My data"
    Address: XXXX
    Data: "My data"

    Name: Ptr1
    Address: YYYY
    Data: XXXX

    Name: Ptr2
    Address: ZZZZ
    Data: XXXX

    Name: Ptr3
    Address: AAAA
    Data: XXXX

    Code:
    char* Ptr2 = ::toString(val).c_str();
    Ptr3 = Ptr2;

    Generated Code:
    Create Ptr2;
    Create temporary std::string.
    std::string::string(); // constructor
    // Begin std::string::string():
    Ptr1 = new char[...];
    // End std::string::string():
    std::string::c_str(); // Call to get pointer.
    Ptr2 = return_value; // Assign returnvalue to Ptr2;
    std::string::~string();
    // Begin std::string::~string():
    delete [] Ptr1;
    // End std::string::~string():
    Ptr3 = Ptr2;

    Ptr1 is used internally by std::string.
    Now std::string calls delete on Ptr1.

    The result will be this:

    Name: "My data"
    Address: XXXX
    Data: UNDEFINED

    Name: Ptr1
    Address: YYYY
    Data: XXXX

    Name: Ptr2
    Address: ZZZZ
    Data: XXXX

    Name: Ptr3
    Address: AAAA
    Data: XXXX

    So now Ptr2 and Ptr3 will all pointer to UNDEFINED, because you copied the address (the pointers) and not the data itself.
    Last edited by Elysia; 07-21-2008 at 10:34 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    So how ?? I am totally Puzzled and frustrated. struggling with it for more than a week.
    I am learning through this Programm.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    See my tutorial above. Perhaps it can shed some light on what's happening.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What foxman tries to say is that the string you generate in toString is a temporary string. It ceases to exist when you leave the function calling toString. It does, however, still point at valid memory, and as long as you don't overwrite the string with something else (string or otherwise), it will still exist in memory.

    You need to allocate a separate piece of memory and copy the string into that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Ya I understood it as I told before just cant figure out the solution Correctly.

    can it be solved by using bcopy()

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    No, you need to look up allocation functions (new/delete) and string copy functions (strcpy).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    LOL Its 100 times simpler to use string as the datatype of __data.
    and I realized that there is 0 profit if I keep it in char* type in this case . (Am I Right ??)
    and Its also working well thus.
    Thanks

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    >> and I realized that there is 0 profit if I keep it in char* type in this case

    I have my reasons to believe that c_str is only there to support the interface certain functions, like file stream constructors, or the open method. Use the string class' interface as much as you can, it's functioning code.

  14. #14
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Thanks you all

  15. #15
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    One more Small question map arranges all alphabetically But I dont need that so whats the alternative ?? that works more or less same as map but doesnt arrange alphabatically automatically.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with friend class declaration in a namespace
    By Angus in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2008, 01:29 PM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 09:32 AM
  3. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 02:39 PM
  4. Static member (map) / constructor initialisation problem
    By drrngrvy in forum C++ Programming
    Replies: 9
    Last Post: 12-28-2005, 12:03 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM

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