hi ,when im trying to insert key-value pair in map(key-char array field of a structure,value-entire structure)im having errors .

NDBAPI/TGBitsNdbApi.cpp: In function `int get_map(Ndb*, char*, char*, void*, void*)':
NDBAPI/TGBitsNdbApi.cpp:154: error: request for member `Msisdn' in `structPtr', which is of non-class type `void*'
NDBAPI/TGBitsNdbApi.cpp:156: error: `void*' is not a pointer-to-object type
NDBAPI/TGBitsNdbApi.cpp:156: error: request for member `Msisdn' in `structPtr', which is of non-class type `void*'
NDBAPI/TGBitsNdbApi.cpp:156: error: `make_pair' was not declared in this scope
TGBitsNdbApi.cpp

#include "TGBitsNdbApi.h"
#include <map>
......
......
int get_map(Ndb * myNdb,char * tableName,char * field,void * structPtr,void *Map)
{
	const NdbDictionary::Dictionary* myDict= myNdb->getDictionary();
        const NdbDictionary::Table *myTable= myDict->getTable(tableName);
        if (myTable == NULL)
                APIERROR(myDict->getNdbError());
	 const NdbDictionary::Index *myIndex= myDict->getIndex("index1",tableName);
	if(myIndex == NULL)
	APIERROR(myDict->getNdbError());

	NdbTransaction *myTransaction= myNdb->startTransaction();
        if (myTransaction == NULL) APIERROR(myNdb->getNdbError());

        NdbIndexOperation *myIndexOp= myTransaction->getNdbIndexOperation(myIndex);
        if (myIndexOp == NULL) 
	{
			std::cout << myTransaction->getNdbError().message << std::endl;
			myNdb->closeTransaction(myTransaction);
			return -1;
	}

	if(myIndexOp->readTuple(NdbOperation::LM_Exclusive) != 0)
	{
			std::cout << myTransaction->getNdbError().message << std::endl;
			myNdb->closeTransaction(myTransaction);
			return -1;
	}
	else
	{
		if(strcmp(structPtr.Msisdn,field)==0)
		{
			Map->insert(make_pair(structPtr.Msisdn,structPtr));//im gettin error in this line
		}
        }
......
......

TGBitsNdbApi.h

#ifndef TGBITSNDBAPI_H_
#define TGBITSNDBAPI_H_
....
....
typedef pair<char *,void *>make_pair;
....
....

HomeNwList.cpp

int main(int argc, char** argv)
{
      .....
      .....
        HomeNwSt homeNwSt;
	map<char *,HomeNwSt> HomeMap;
	map<char *,HomeNwSt>::iterator it;
        int val=get_map(Ndb * myNdb,char * tableName,char * field,(void *)&homeNwSt ,(void*)&HomeMap)
	      if(val==-1)
              ........
              ........
	for( it = HomeNwList.begin(); it != HomeNwList.end(); it++)
	   {
		
		HomeNwSt obj = (*it).second;
                std::cout << "col 1: "<< obj.NwId << "\t";
                std::cout << "col 2: "<< obj.Msisdn << "\t\n";
	   }
	return 0;
}

HomeNwList.h

#ifndef HOMENWLIST_H_
#define HOMENWLIST_H_

#include "./NDBAPI/TGBitsNdbApi.h"
#include<map>
#include<utility>
typedef struct _HomeNwSt
{
	char Msisdn[20];
	int   NwId;
}HomeNwSt;

//typedef pair<char *,void *>make_pair;
....
....
#endif

i do understand it is showing this error because im sending the entire structure,map as void * in get_map() function and im trying to insert char array field of structure.i did tried typecastin it into char * which im not sure whether it is correct,but it was not working.so anyone could help me in fixing this error and explain me,as im not good in pointers.
Also,i didnt get the last error ,as i declared the make_pair in correct scope and i also checked spelling.

thanks

> //im gettin error in this line
You're also getting an error in the line before.

Namely
if(strcmp(structPtr.Msisdn,field)==0)

structPtr is void*, so there the NO .something or ->aThing you can do with a void* pointer.
If you're going to use void*, then at least past the correct type into the function, and cast it to void at the last possible moment.


Why isn't it a map of std::string and HomeNwSt ?

All this char* and void* is not good C++

hi one correction in the above code,in the HomeNwList.cpp,inside the for loop it is actually :

for( it = HomeMap.begin(); it != HomeMap.end(); it++)

it is not

for( it = HomeNwList.begin(); it != HomeNwList.end(); it++)

hi salem based on your comment i used std::string and tried to typecast it into correct type like this-

if(strcmp(std::string((char *)structPtr.Msisdn),field)==0)
                {

                        Map->insert(make_pair(std::string((char *)structPtr.Msisdn),structPtr));
                }

in the above code i know that im converting the entire structure into char * which makes the other int field in the structure also char * because of which im gettin the same error again.im getting stuck here as i dont know exactly how to typecast here and get the correct value ,so if u could help me out here it would be great.

thanks

Er - no.
Start with the premise that your simple C++ program requires ZERO casts and work from there.

Random casting just to silence the compiler will NOT make it work.

For instance, replace ALL your char arrays with std::string.

hi salem,i have changed char array fields into std::string in structure declaration ,i also changed the same in map & map iterator declaration.now i tried above code like this :

Map->insert(make_pair(structPtr.Msisdn,structPtr));

but it was showing error like this:
NDBAPI/NdbApi.cpp:157: error: `void*' is not a pointer-to-object type
NDBAPI/NdbApi.cpp:157: error: request for member `Msisdn' in `structPtr', which is of non-class type `void*'
salem i've doubt how std::string would allocate memory space for the equivalent of
char[20] Msisdn;
by declaring
std::string Msisdn;
without specifying the size we need or does it allocate at runtime?
sorry to say that i dont need to write the below code:

if(strcmp(std::string((char *)structPtr.Msisdn),field)==0)

as for my functionality it is wrong to check.so i deleted those lines.

> without specifying the size we need or does it allocate at runtime?
It's done for you, safely, at run-time.

I suggest you have a nice long read of the STL background information.

ok sure i'll read about it ,but can you tell me how to cast it to correct type,as i did also tried:

Map->insert(make_pair((std::string)structPtr.Msisdn,structPtr));

but was showing the same error...

thanx

ok salem if it is so then why compiler is showing the same error when i write like this:

Map->insert(make_pair(structPtr.Msisdn,structPtr));

i've also included how i've declared everything,so that you may get where im going wrong...
i wrote my structure declaration like this:

typedef struct _HomeNwSt
{
        std::string Msisdn;
        int   NwId;
}HomeNwSt;

map &map iterator declaration :

map<std::string,HomeNwSt> HomeMap;
map<std::string,HomeNwSt>::iterator it;

make_pair typedef :

typedef std::pair <std::string,void *> make_pair;

but in the function in which im trying to insert fields into map ,im passing this entire structure and map as void * parameters like this:

int get_map(Ndb * myNdb,char * tableName,char * field,void * structPtr,void *Map)

thanx

> int get_map(Ndb * myNdb,char * tableName,char * field,void * structPtr,void *Map)
Rough guess - all the void* you've still got.

salem im not that good in pointers and have only vague idea about using it ,so i think i have no other option other than to pass them(structure and map) as void *.
if you think there are other possible ways to get them inside this function please let me know.

thanx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.