Thread: STL multimap error

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663

    STL multimap error

    Hi,

    I'm trying to get a simple map example from "The C++ Standard Library"(Josuttis) to work, but I'm getting an error I can't figure out. When I click on the error message it takes me to the pair constructor template.

    error C2536: 'std::pair<int,char [7]>::second' : cannot specify explicit initializer for arrays
    c:\program files\c++microsoft visual studio\vc98\include\utility(26) : see declaration of 'second'
    c:\program files\c++microsoft visual studio\vc98\include\utility(21) : while compiling class-template member function '__thiscall std::pair<int,char [7]>::std::pair<int,char [7]>(const int &,const char (&)[7])'
    Error executing cl.exe.

    Code:
    #pragma warning( disable : 4786 ) //to get rid of STL warnings
    
    #include<iostream>
    #include<map>
    #include<string>
    using namespace std;
    
    int main()
    {
    	typedef multimap<int,string> intStrMMap;
    	
    	intStrMMap coll;
    
    	coll.insert(make_pair(5, "tagged"));
    	coll.insert(make_pair(2, "a"));
    	coll.insert(make_pair(1, "this"));
    	coll.insert(make_pair(4, "of"));
    	coll.insert(make_pair(6, "strings"));
    	coll.insert(make_pair(1, "is"));
    	coll.insert(make_pair(3, "multimap"));
    
    	intStrMMap::iterator pos;
    	for(pos = coll.begin(); pos != coll.end(); ++pos)
    	{
    		cout<<pos->second<<' ';
    	}
    
    	cout<<endl;	
    	
    	return 0;
    }
    Last edited by 7stud; 11-21-2005 at 11:25 PM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Is this all your compiling? Cause it compiles fine for me. May I ask what compiler you're using?

    EDIT: Heh nevermind, I see in the error address.
    Sent from my iPad®

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Sorry, VC6. I meant to include that since it seems like the problem could be compiler related.

    This works:
    Code:
    #pragma warning( disable : 4786 ) //to get rid of STL warnings
    
    #include<iostream>
    #include<map>
    #include<string>
    
    
    using namespace std;
    
    
    int main()
    {
    	typedef multimap<int,string> intStrMMap;
    	
    	intStrMMap coll;
    
    	coll.insert( make_pair(5, string("tagged")) );
    	coll.insert( make_pair(2, string("a")) );
    	coll.insert( make_pair(1, string("this")) );
    	coll.insert( make_pair(4, string("of")) );
    	coll.insert(make_pair(6, string("strings")) );
    	coll.insert(make_pair(1, string("is")) );
    	coll.insert(make_pair(3, string("multimap")) );
    
    	intStrMMap::iterator pos;
    	for(pos = coll.begin(); pos != coll.end(); ++pos)
    	{
    		cout<<pos->second<<' ';
    	}
    
    	cout<<endl;	
    	
    	
    	return 0;
    }
    I guess it makes sense that make_pair() doesn't realize I want the second parameter to be a string type rather than a char array. But, I wonder why the multimap can't covert it.
    Last edited by 7stud; 11-21-2005 at 11:26 PM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    This works too:

    coll.insert( make_pair<int, string>(5, "tagged") );

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    One thing to be aware of is that VC6 does have some issues with the STL - though obviously this wasn't one of them. I think you can get the STLPort library to work with VC6 though, which can alleviate some of the issues.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But, I wonder why the multimap can't covert it.
    Because it never gets that far. The string literal "tagged" is, as the error message says, of type char[7]. As a result, make_pair creates a std:air<int, char[7]>.

    The template std:air contains this constructor:
    Code:
    template<typename T1, typename T2> class pair {
    ...
      T1 first;
      T2 second;
    
    public:
      pair(T1 &t1, T2 &t2)
        : first(t1), second(t2)
      {
      }
    };
    This expands to this:
    Code:
    pair(int &t1, char [7] &t2)
      : first(t1), second(t2)
    {
    }
    But char arrays don't have constructors, not even the kind that primitives have. That's why the code fails to compile.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Because it never gets that far.
    Well, that doesn't sound like a compiler issue then--rather an error in the example. However, it's not listed in the online errata for the book.

    Does it compile for you? SlyMaelstrom says it did for him/her.
    Last edited by 7stud; 11-22-2005 at 01:48 PM.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Works in VC++7.1, so perhaps the "arrays don't have constructors" is a VC++6 issue.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    VC++ 2005 Express edition is a free download, and it includes the IDE, debugger and optimizing compiler that is of course much more standards compliant than VC 6 (or so I hear, I haven't tried it yet).

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yes, it is. Since VC++ 2003, it's a LOT better.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    VC++ 2005 Express edition is a free download, and it includes the IDE, debugger and optimizing compiler that is of course much more standards compliant than VC 6 (or so I hear, I haven't tried it yet).
    What does VC++ 2005 not include?

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The express edition doesn't include MFC and some other libraries (I think). It also might not have all of the IDE specialness that the full version has. I haven't tried it myself yet, but it sounds like it would be worth a try as a free upgrade over 6.0.

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Thanks for all the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM