Assigning an std::set as a tree's node problem

This is a discussion on Assigning an std::set as a tree's node problem within the C++ Programming forums, part of the General Programming Boards category; Hi, I am having trouble creating a std::set as the datamember in an expression tree's node. Here is what I ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    14

    Assigning an std::set as a tree's node problem

    Hi,

    I am having trouble creating a std::set as the datamember in an expression tree's node.

    Here is what I have:
    Code:
    //an std::map is where the sets are held
    map<string,set<int> > mapname;
    
    //an iterator to the map
    map<string,set<int> >::iterator mapnameit;
    
    //create a std::set
    set<int> setone;
    
    setone.insert(1);
    setone.insert(2);
    setone.insert(3);
    
    string keyone = "a1";
    
    mapname[keyone] = setone;
    
    //Now have a std::map containing one element of: <string,set<int> >
    
    //Find the element in the map
    mapnameit = mapname.find("a1");
    
    //call the node constructor, setting the map's std:: set data member ( 1,2,3 ) as 
    //the nodes data member
    ExTree* mynode = new ExTree(*mapnameit);
    
    //Now the node should contain the set {1,2,3}, but fails to build.
    I'm stuck with trying to assign the set from the map as the nodes value.

    Visual Studio says:
    cannot convert parameter 1 from 'std:air<_Ty1,_Ty2>' to 'std::string'


    Thanks for any help, it's most appreciated
    Last edited by Orange Lozenge; 01-25-2011 at 03:12 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So, look at your error message: it is trying to convert a std::pair<> into a std::string. And if you look at the bolded line, *mapnameit is definitely a pair (being the dereference of a map iterator). Therefore you need to make a constructor for ExTree that accepts a std::pair<> item, instead of just the one that you apparently have that takes a std::string.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Why is your ExTree constructor expecting to take a string?
    If you think that dereferencing the mapnameit iterator gives you a string, then wouldn't you also expect that string to be "a1", since that's the item you told it to find? It then makes no sense to call find at all since you are expecting to get what you started with.

    In fact dereferencing the iterator actually gives you a std::pair so you can access both the key and the mapped value. use ->first to get the key (int your case a string) and ->second to get the mapped value (in your case a set).
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help a Student in C (Linked List, Pointers etc)
    By mocha1218 in forum C Programming
    Replies: 14
    Last Post: 07-02-2010, 01:38 PM
  2. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 09:16 AM
  5. Memory Problem - I think...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-24-2001, 12:14 PM

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