std::map declaration problem

I am having a problem with the code that inserts an item into a map. I think it is really a decleration problem. I use typedef the set it up because the map is used in two classes.

I cannot cut and paste from my work computer for various reasons so please comment on any typos, but presumed I made an entry error. There is only one compiile error in the program and that is on the line marked as such.

From the .h file:
1
2
3
4
5
6
7
8
9
typedef   int td_msg_parameter_number;
typedef  CString  td_msg_parameter_name;
typedef std::map< td_msg_parameter_number, td_msg_parameter_name > td_map_parameter_names;
typedef std::map< td_msg_parameter_number, td_msg_parameter_name >:: iterator td_iterator_parameter_names;

typedef struct
{  ...
   td_map_parameter_names *pmap_message_parameters;
}


Then from the .cpp file I try to insert a name into the map.
The method Get_Next_Definition(..) reads a line of text from a configuration file and returnes two strings of data. The second string is to be put in the map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void C_Configuration_Manager::Populate_Parameter_Map( td_map_parameter_names * pmap_message_parameters )
{

td_message_parameter_number  parameter_number = 1;
td_msg_parameter_name            cannot_use;
string                                         expected;
string                                         t_string;
bool                                           call_status;

do{
expected = "Parameter";
call_status = Get_Next_Definition( &expected, &t_string );
if( t_string != "End" )
   {
//      cannot_use( t_string.c_str() );  // why not, secondary question
    td_msg_parameter_name local_parameter_name( t_string.c_str() );
        // this compiles

       // I check the help file and added this just to test it.  No compile error
    pmap_message_parameters ->insert( map<int, CString>:: value_type( pararmeter_number, local_parameter_name ) );

          // This is my problem line.
      pmap_message_parameters[ parameter_number ] = local_parameter_name; 
}
} while( t_string != "End" );
}


the error returned is:
Error 1 error C2697: binary "=' : no operator found which takes a right-hand operand of type 'td_msg_parameter_name' (or there is no acceptable conversion)

Last edited on
This is because you are assigning an object and not a pointer.

Your secondary question is related: t_string.c_str() returns a null-terminated string, not a CString.
I don't understand. the declaration:
1
2
3
4
typedef   int td_msg_parameter_number;
typedef  CString  td_msg_parameter_name;
typedef std::map< td_msg_parameter_number, td_msg_parameter_name > td_map_parameter_names;
 

I see the map as using an integer as the key and a CString as the item stored. Neither are pointers.

When you say an object and not a pointer are you referring to
pmap_message_parameters[ parameter_number ]
or to
local_parameter_name;

Thank you
Sorry to be unclear (I usualy am to some degree): On line 23, I guess you don't have a copy constructor or operator = for pmap_message_parameters, so the compiler says there isn't one. Try this for the line:

1
2
3

pmap_message_parameters[ parameter_number ] = &local_parameter_name;


The c_str() is not returning a CString, but the data to which that object points ( char *).
I added the address operator to produce:

pmap_message_parameters[ parameter_number ] = &local_parameter_name;

and the error message changed slightly to:

Error 1 error C2697: binary "=' : no operator found which takes a right-hand operand of type 'td_msg_parameter_name *' (or there is no acceptable conversion)


As I look at the map declaration I don't see that there is a pointer involved. I presume that it will take the local_parameter_name as "by value" (as opposed to "by ref" or "by address") and copy local_parameter_name into the map. I am still seeing this as a declaration problem and don't understand my error.


Edit:
I backed up to the method where I created the map and tried to insert a simple value. That looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
 
    // I start with a structure for a message.  The message contains multiple parameters.
    // the goal is to create a map for those parmeters and use the parameter number
   //  as the key into the map.
p_message_definition->pmap_message_pararmeters = new ( td_map_parameter_names );

    // Create test data to put in the map
td_msg_parameter_name   stuff = _T("stuff" )
td_msg_parameter_number  parameter_number = 1;

    // Now try to add a single entry into the map

p_message_definition->pmap_message_pararmeters[ parameter_number ] = stuff;


Did that simplify the question or did I just add an error on top of another error?
Last edited on
Well bkelly, I guess I'm getting too old for this! :)

Perhaps the problem is with the "left" side: pmap_message_paraameters is a pointer, no?
So maybe the compiler wants (in your original post):

1
2
3
4
5
6
7

*pmap_message_parameters[ parameter_number ] = local_parameter_name;

or perhaps:

&pmap_message_parameters[ parameter_number ] = local_parameter_name;
The immediate problem appears to be one of precedence:

(*pmap_message_parameters)[ parameter_number ] = local_parameter_name;

Add in the parens and it compiled. But then, I still didn't have what I needed. I eventually detrermined that I really needed to pass an pointer pointer to a function so it could give me a pointer to the map.

Just a few days ago I worked with one doing CreateDIBSection and figured out what that really means. Then yesterday I saw that I needed just that and and was able to create it.

Thank you for taking the time to reply.
You're most welcome bkelly! These pointer to pointer issues always seem to bite me. You'ed think I would have it down since I've been programming in C since 1986 and C++ since 1991. Sheesh!

Thank you for your patience with me!
Topic archived. No new replies allowed.