PDA

View Full Version : no matching function for call to object map(*it);



ejoshva
10th March 2015, 10:55
I am trying to make call to a function multiple times but parallelly using QtConcurrent::mapped

But I am not able to over come below error.

error: no matching function for call to object of type 'addWrapper' *result = map(*it); ^~~ The code is as follows


struct addWrapper {
loadbook *instance;
typedef QString result_type;
addWrapper(loadbook *lBook) : instance(lBook) {}
QString operator()(QString &item) {
instance->decrypt->decryptFile(item,'A');

}
};

addWrapper wrapper(this);

QFuture<QString> returnContent = QtConcurrent::mapped(fileList,wrapper);
kindly help me resolve this issue.

wysota
10th March 2015, 11:47
Why are you complicating things so much?


#include <QtConcurrentMap>
#include <QStringList>
#include <QByteArray>
#include <QFile>

struct Decrypter {
Decrypter(const QByteArray &key) : m_key(key) {}

typedef QByteArray result_type;

QByteArray operator()(const QString &fileName) {
QFile f(fileName);
if(!f.open(QIODevice::ReadOnly)) return QByteArray();
QByteArray result;
result.reserve(f.size());
int keyIndex = 0;
char input;
char keyByte;
while(!f.atEnd()) {
f.getChar(&input);
keyByte = m_key.at(keyIndex);
keyIndex = (keyIndex+1) % m_key.size();
result.append( input ^ keyByte );
}
return result;
}
private:
QByteArray m_key;
};

int main() {
QStringList filesToDecrypt = { "file1", "file2", "file3" };
QFuture<QByteArray> future = QtConcurrent::mapped(filesToDecrypt, Decrypter("abcdef"));
return 0;
}

anda_skoa
10th March 2015, 11:51
I think you have a mismatch in your function signature.
Try a const reference for the operator argument.

Cheers,
_

ejoshva
11th March 2015, 04:06
Thank you wysota and anda_skoa for you help.

@Wysota, I have made my decryt() re-entrant.


QString Decrypt::decryptFile(QString sourceFile)
{
QString decryptedContent;
decryptedContent.clear();
string srcFile=sourceFile.toUtf8().constData();
ifstream ffin;

ffin.open(srcFile.c_str(),ios_base::in|ios_base::b inary);
QChar keystring = 'A';
char keyToEncryption= keystring.toLatin1();
if(ffin.is_open())
{
char ch;
while(!(ffin.read(&ch,1).eof()))
{
ch ^= keyToEncryption;
decryptedContent.append(ch);
}
}
ffin.close();
return decryptedContent;

}

QString Decrypt::decryptFile(QString sourceFile,QChar keystring)
{
_decrptedResStr.clear();
string srcFile=sourceFile.toUtf8().constData();
ifstream ffin;

ffin.open(srcFile.c_str(),ios_base::in|ios_base::b inary);

char keyToEncryption= keystring.toLatin1();
if(ffin.is_open())
{
char ch;
while(!(ffin.read(&ch,1).eof()))
{
ch ^= keyToEncryption;
_decrptedResStr.append(ch);
}
}
ffin.close();
return _decrptedResStr;
}



I didn't realise what you were telling. Only upon pondering what's wrong got the issue. Now have modified to the first function above. Earlier it was of type 2nd function.

Thank you so much

Added after 16 minutes:

@anda_skoa : Thank you so much. You have pointed out exactly. It works now.



QFuture<QString> decryptedContent = QtConcurrent::mapped(fileList,DecryptMap('A'));

struct DecryptMap {
DecryptMap(const QChar &key) : m_key(key) {}
typedef QString result_type;

QString operator()(const QString &item)
{
Decrypt *decrypt = new Decrypt();
return decrypt->decryptFile(item,'A');
}
QChar m_key;
};

anda_skoa
11th March 2015, 07:43
QString operator()(const QString &item)
{
Decrypt *decrypt = new Decrypt();
return decrypt->decryptFile(item,'A');
}
};

You are leaking the Decrypt instance, i.e. there is a "new" but no "delete".
Better create it on the stack, i.e. without new, then it is destroyed when the operator's scope ends.

Cheers,
_

ejoshva
11th March 2015, 09:30
@anda_skoa : thanks for pointing it out. will take care now