PDA

View Full Version : Problem During fatching records from QMAP container



lekhrajdeshmukh
27th October 2011, 10:43
Hi All,

I have one basic doubt that i have written one program and placed my map conatiner on one class and now i am trying to fetch records from another class with reference of existing class on which map conatiner is defined.But problem is that i am not getting records from different class.

Following are the code

Header definition for class on which i have defined MAP container
#ifndef MAPCONFIG_H
#define MAPCONFIG_H
#include <QString>
#include <QMap>

class MapConfig
{
public:
void mapconfig();
QMap<QString,QString> map;
QString find_key;
QString msisdn_ip;

};
#endif // MAPCONFIG_H

class Definition for above class
#include <QFile>
#include <QMessageBox>
#include <QString>
#include <QStringList>
#include <QDebug>
#include <QMap>

#include "mapconfig.h"
#include "findkey.h" //this is another class on which i am trying to fetch records from map container
void MapConfig::mapconfig()
{
FindKey ob;
QFile file("D:/CDR-LOADER/181011/config.txt");
QString str;
QStringList str_list;
if(!file.open(QIODevice::ReadOnly|QIODevice::Text) )
{
QMessageBox::information(0,"Error","Error in opening file");

}
map.clear();
while(!file.atEnd())
{
str = file.readLine();
str_list = str.split("|");
QString str_key = str_list.at(0) + str_list.at(1);
QString str_value = str_list.at(2);
qDebug()<<str_key<<" "<<str_value;

map.insert(str_key,str_value);
}
file.close();
find_key = "TS1";
msisdn_ip = "6984";
ob.findkey(find_key); //Passing "TS1" as key argument on findkey function
}

//header definition for FindKey Class

#include <QString>
class FindKey
{
public:
void findkey(QString str_key);
};

//Class Definiton for FindKey Class

#include <QFile>
#include <QMessageBox>
#include <QString>
#include <QStringList>
#include <QDebug>
#include "findkey.h">
#include "mapconfig.h"

void FindKey::findkey(QString str_key)
{
mapconfig ob1;
qDebug()<<"Inside Find Key"<<":"<<str_key; //Here its giving string value as "TS1"

QString str_query;
if(map.contains(str_key))
{
str_query=ob1.map.value(str_key);
qDebug()<<str_query;

}
else
{
QMessageBox::warning(0,"Error","No value available for input key");

}
}
qDebug of mapconfig class has given me value for key "TS1" but findkey class giving me ","No value available for input key".
Can anybody help me why else part of code is executing. If i place this code inside same class on which i have placed mapconfig then code work properly.

Please appologies me if it seem as dumb question but i am new on development and just started coding in QT.

Thanks in advance.

Lekhraj Deshmukh

MarekR22
27th October 2011, 12:06
Basics of C++ say hello!
Your method void MapConfig::mapconfig() is never called since it is not a cunstructor and it is never invoked by your code! C++ is case sensitive so "MapConfig" and "mapconfig" are completely different names, ergo "MapConfig::mapconfig()" it is not a constructor (it looks like this was your intention).

lekhrajdeshmukh
27th October 2011, 13:12
Thanks Marek,got your point.