Loud thinking about compiler behaviour

Hi All,

First of all I am new user so pardon me for any inappropriateness in the post.
Let me make it clear that this general C++ compiler related post not Nokia Qt specific!

I was using a class from Nokia Qt framework with following declaration:

1
2
3
4
5
6
7
8
class Q_GUI_EXPORT QImage : public QPaintDevice
{
public:
    ...
    bool save(const QString &fileName, const char* format=0, int quality=-1) const;
    bool save(QIODevice *device, const char* format=0, int quality=-1) const;
    ...
};


1
2
3
4
class Q_CORE_EXPORT QBuffer : public QIODevice
{
...
};

Now following is a snippet of my code using two above-mentioned classes.
1
2
3
4
5
6
7
8
9
10
11
const QString THUMB_FORMAT = QString("JPG");
const int THUMB_QUALITY = -1;
...
    QByteArray bytes ;
    QBuffer buffer(&bytes);
    QImage image = createThumbnailImage(...);
    buffer.open(QIODevice::WriteOnly);
    if(!image.save( &buffer, THUMB_FORMAT, THUMB_QUALITY)) { // <-- this line is where compilation failed.
        return false;
    }
...

This code doesn't compile with error:
1
2
3
 error C2664: 'bool QImage::save(const QString &,const char *,int) const' : cannot convert parameter 1 from 'QBuffer *' to 'const QString &'
        Reason: cannot convert from 'QBuffer *' to 'const QString'
        No constructor could take the source type, or constructor overload resolution was ambiguous.


Now if I replace, the line in question with
if(!image.save( &buffer, THUMB_FORMAT.toStdString().c_str(), THUMB_QUALITY)) {
it compiles, fine, which is obvious! :)

But I was wondering
1. compiler shouldn't have shown error message pointing at the first declaration of save method
2. it should have pointed to second declaration of save for 2nd parameter where it expects a "const char*" but receiving a QString.

Shouldn't it try to show the one that like to match better?

What do you think? Please let me know.

Thank you.

Best Regards,
Rupesh B

The compiler error that you list is saying that you don't have a member named save() in QImage that takes a QBuffer *. I don't see how it compiled with the change you made in the second example. I agree that the second parameter is now correct in the second version of your code.
It compiled because QBuffer is derrived from QIODevice and save has an overloaded function that takes QIODevice*.
Topic archived. No new replies allowed.