PDA

View Full Version : Type conversion on container templates



Alir3z4
27th January 2012, 23:42
Actually i did alot changing on code and googling, if it's make you feel better, i suffering myself to don't ask a question!
But i have to do :|

I have c++ class with a this function

std::string addUri(std::vector<std::string> uris, std::map<std::string, std::string> options);
All i want to do is make a qt wrap function for it
Like:

QString addUri(QVector<QString> uris, QMap<QString, QString> options);:confused:
The wrap one isn't right
I will apperciate to help me in this conversion
tahnks

ChrisW67
27th January 2012, 23:58
What's "not right" about it?

amleto
28th January 2012, 13:11
passing std::vector<std::string> uris, std::map<std::string, std::string> options by value is a lot wwrse than passing most Qt types by value. You should be passing stl containers by const ref, not value.

If you conversion from stl to Qt is not correct, then you should show us your attempt at that code.

Alir3z4
28th January 2012, 14:03
Here you can see the source code, it's on the git
yoDownet/yoDownloaders/aria2c.h (http://sourceforge.net/p/yodownet/git/ci/8f654f05082cc7622c7712abd9b680d196c8825e/tree/yoDownloaders/aria2c.h)
aria2c.h is a c++ source, which i wan't to make a qt function wraper for it.
there is a qt/class called Downloader which my program access to aria2c.h throght it.
i don't know how to implement such function to handle type conversion.

QString addUri(QVector<QString> uris, QMap<QString, QString> options);
i hope i explained my point as well

##update:
i think i didn't push the local git repo to remote, that's why the Downloader class isn't available on the git :|

amleto
28th January 2012, 15:43
There are several steps:
How do you convert QString to std::string?
How do you convert QVector to std::vector?
How do you convert QMap<QString, QString> to std::map<std::string, std::string>?

QString qs("abc");
std::string x(qs.toStdString());

QVector<QString> = qsv;
qsv.push_back(qs);

std::vector<std::string> sv;
sv.push_back(qs.toStdString());

or

sv.resize(qsv.size());
sv[some_index] = qsv[some_index].toStdString();

etc.

if you feel adventurous, you can do it in very few lines with std::transform and a suitable functor

Alir3z4
29th January 2012, 17:58
The actual thing i wanna do is making conversion at the template argument, not getting them one by one!
like:

const QString addUri(const QVector< const std::string(QString::toStdString()) > uris);

huh? what are you talking about dah? :confused:
but all i've got every time is delicious error :|

wysota
29th January 2012, 18:24
I'm sorry, what exactly is the problem? You have problems with function signature or with function body? What is the error you are getting?

Alir3z4
29th January 2012, 18:28
I'm sorry, what exactly is the problem? You have problems with function signature or with function body? What is the error you are getting?

i didn't even implement the function body
this is the error!


make: Entering directory `/home/alireza/dev/qt/yoDownet-build-desktop-Qt_in_PATH_Debug'
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_SQL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt/mkspecs/linux-g++ -I../yoDownet -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include/QtSql -I/usr/include -I. -I. -I../yoDownet -I. -o downloader.o ../yoDownet/downloader.cpp
In file included from ../yoDownet/downloader.cpp:1:0:
../yoDownet/downloader.h:17:77: error: template argument 1 is invalid
make: Leaving directory `/home/alireza/dev/qt/yoDownet-build-desktop-Qt_in_PATH_Debug'
make: *** [downloader.o] Error 1
22:36:00: The process "/usr/bin/make" exited with code 2.
Error while building project yoDownet (target: Desktop)
When executing build step 'Make'

Don't tell me give us the source code, coz the whole header file goes to 5 lines :|

wysota
29th January 2012, 21:04
And the exact line triggering the error (downloader.h:17 and :77)?

Alir3z4
29th January 2012, 22:27
And the exact line triggering the error (downloader.h:17 and :77)?
== give me the source code :|

#ifndef DOWNLOADER_H
#define DOWNLOADER_H

#include <QObject>
#include <QString>
#include <QVector>
#include <QMap>
#include "yoDownloaders/aria2c.h"

class Downloader : public QObject
{
Q_OBJECT
public:
explicit Downloader(QObject *parent = 0);

// Operations :|
const QString addUri(const QVector<QString> &uris);
QString addUri(QVector<QString> &uris, QMap<QString, QString> &options);

private:
Aria2c aria2;


};

#endif // DOWNLOADER_H


i make a function for it actually to make things work :|, but this is for QMap<QString, QString>
i don't know it's working or not yet!

const std::map<std::string, std::string> Aria2c::fromQMap(const QMap<QString, QString> &q_map)
{
std::map<std::string, std::string> std_map;
QMap<QString, QString>::const_iterator i = q_map.constBegin();
while (i != q_map.constEnd()) {
std_map.insert(std::pair<std::string, std::string>(i.key().toStdString(), i.value().toStdString()));
}

return std_map;
}

just for information, it works :D

wysota
29th January 2012, 22:29
This builds correctly:


#include <QObject>
#include <QString>
#include <QVector>
#include <QMap>

class Downloader : public QObject {
Q_OBJECT
public:
explicit Downloader(QObject *parent = 0) {}

const QString addUri(const QVector<QString> &uris);
QString addUri(QVector<QString> &uris, QMap<QString, QString> &options);

};

#include "main.moc"

int main(){
return 0;
}

so either your yoDownloaders/aria2c.h is the problem or your building script (.pro file?) is broken.

Try commenting out the two lines of code concerning aria2c and see if it changes anything.

By the way, you really shouldn't be passing non-const references to addUri...

And about your reluctance to show us your code --- you don't have to show us your code... and we don't have to show you ours (nor share our knowledge with you, in the end it's all available on a silver platter in the docs).

Alir3z4
29th January 2012, 22:40
of course it's working now, because i change it and make it qt/c++
the downloader class is completely implemented with qt classes,
this is what i want actually

#ifndef DOWNLOADER_H
#define DOWNLOADER_H

#include <QObject>
#include <QString>
#include <QVector>
#include <QMap>
#include "yoDownloaders/aria2c.h"

class Downloader : public QObject
{
Q_OBJECT
public:
explicit Downloader(QObject *parent = 0);

// Operations :|
const QString addUri(const QVector< const std::string(QString::toStdString()) > uris);
QString addUri(const QVector< const std::string(QString::toStdString()) > &uris, QMap<const std::string(QString::toStdString()), const std::string(QString::toStdString())> &options);

private:
Aria2c aria2;


};

#endif // DOWNLOADER_H
I know it's weird, or mybe not

But the main question is how do make conversion like that. how do make conversion like QVector<QString, QString> => std::vector<std::string, std::string>
By the way i solve my problem with changing the yoDownloaders/aria2c codes and make it work with Qt types.

ChrisW67
29th January 2012, 22:41
Your first listing: The error message indicates that at line 17 column 77 the compiler found the error described. None of the lines in this header file are 77 characters long, and none of the lines contain an obvious error. If I replace the aria2c.h include with a null definition of the Aria2c class then the code passes through my compiler no problems. Have you posted the actual code, or just sort-of the code?

Your second listing: What does this have to do with the posted error?

Edit: Ignore me...

wysota
29th January 2012, 22:53
how do make conversion like QVector<QString, QString> => std::vector<std::string, std::string>


QVector<QString> qstrvec = ...;
std::vector<std::string> stdstrvec;
foreach(const QString &str, qstrvec) {
stdstrvec.push_back(str.toStdString()); // note, QString is Unicode, std::string is not so the conversion is lossy
}

Alir3z4
29th January 2012, 22:57
i solved my problem somehow, you can check it yodownet/...779995c/log/ (http://sourceforge.net/p/yodownet/git/ci/30c70f06f439602b41919b0f87adcc3b8779995c/log/) if you like ## check the 2 last changes
but the main question is on also

how do make conversion like QVector<QString, QString> => std::vector<std::string, std::string>


Ignore me...
;)



QVector<QString> qstrvec = ...;
std::vector<std::string> stdstrvec;
foreach(const QString &str, qstrvec) {
stdstrvec.push_back(str.toStdString()); // note, QString is Unicode, std::string is not so the conversion is lossy
}
No, why nobody wanna feels me :D
I mean at the function signature

i made some function already
https://gist.github.com/1700075
https://gist.github.com/1700559

wysota
29th January 2012, 23:05
I think you need to express your problems in a somewhat more understandable fashion. If your post above says you still have a problem then I can't see where. I don't know what you mean by "I mean at the function signature". Nobody "wanna feels you" at the function signature?

Alir3z4
29th January 2012, 23:14
Nobody "wanna feels you" at the function signature?
Oh com'on, i make it gray at put that smiley to say it for humor sake!

how should i express my problem is somehow/somewhat/some??? more understandable fashion?
i explain it already :D, make type conversion at the function signature not by another function

const QVariant seeMyFace(const QMap<QString, int> ==> std::map<string, int>);
i mean that little guyz

QMap<QString, int> ==> std::map<string, int>

wysota
29th January 2012, 23:53
make type conversion at the function signature not by another function
I'm sorry but this does not make any sense in terms of exchanging information between two entities. "To do something at the function signature" is not a proper English statement and I can't understand it. You can see that you used such statements twice and you have been misunderstood by at least two distinct people. This should ring a bell in your head that something is wrong with the way you are communicating.



const QVariant seeMyFace(const QMap<QString, int> ==> std::map<string, int>);
i mean that little guyz
Neither does this make any sense. This is neither C++ code nor pseudo-code nor anything even remotely similar to any meaningful construction in any human-spoken language (at least those I'm aware of).

So if you want help then please express yourself clearly using full and proper English sentences. This is the best way towards getting understood. If you want an apple then say so instead of saying "I wanna this roundy redish stuff, you know".

"I mean that little guyz" would suggest you were talking about dwarfs but I can't see anything related to dwarfs in this thread.

Alir3z4
29th January 2012, 23:56
I'm sorry but this does not make any sense in terms of exchanging information between two entities. "To do something at the function signature" is not a proper English statement and I can't understand it. You can see that you used such statements twice and you have been misunderstood by at least two distinct people. This should ring a bell in your head.


Neither does this make any sense. This is neither C++ code nor pseudo-code nor anything even remotely similar to any meaningful construction in any human-spoken language (at least those I'm aware of).

So if you want help then please express yourself clearly using full and proper English sentences. This is the best way towards getting understood. If you want an apple then say so instead of saying "I wanna this roundy redish stuff, you know".
== learn english
:|
i solved it, let finish this off ;)