View Full Version : QString conversion function
Bziur
4th March 2014, 21:41
Hello. I'm trying to make my work easier by overloading QString() conversion function:
private:
QString* c_string;
public:
operator QString() {
if( c_type == STRING )
return *c_string;
else
return toString();
}
then I'm trying to use it like this
QString line(int i) const {
return c_labels[i] + ": " + c_values[i]; // c_labels[i] returns QString, c_values[i] returns the object with the above conversion function.
}
and I'm getting error: call of overloaded 'QString(const IniParam&)' is ambiguous
I also tried putting ": " in QString(), same for c_values[i], <QString> before c_values[i] doesn't help either;
Any Solutions?
ChrisW67
5th March 2014, 02:22
We currently cannot see enough. Please post a small, complete example that fails to compile in this way.
Bziur
5th March 2014, 11:49
Ok so I figured out it happens when you overload some different conversion functions;
Any solution to that? I wanna be able to easily output to multiple formats.
convertable.h
#ifndef CONVERTABLE_H
#define CONVERTABLE_H
#include <QString>
class Convertable
{
private:
int i;
QString c_string;
public:
Convertable(int in=0, const QString string = QString()):c_string(string),i(in) {}
operator int()
{
return i;
}
operator QString()
{
return c_string;
}
};
#endif // CONVERTABLE_H
main.cpp
#include <QCoreApplication>
#include "convertable.h"
#include <QDebug>
QString toString(const QString &in1, Convertable &in2)
{
return in1 + ": " + in2;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString A("Print");
Convertable B("something");
qDebug() << toString(A,B);
return a.exec();
}
Radek
5th March 2014, 16:10
Please specify what all is wrong now. At least, the following will not pass:
Convertable B("something");
You do not have a ctor for it. Try
Convertable B(0,"something");
Bziur
5th March 2014, 16:20
I forgot to change that but it doesn't matter much as it's further down the issue list.
The error I'm getting:
C:\Test\main.cpp:8: error: ambiguous overload for 'operator+' (operand types are 'const QString' and 'Convertable')
return in1 + ": " + in2;
^
Radek
5th March 2014, 17:04
This can be caused by the int conversion of Convertable because there exists operator + ( const QString& str, char chr ). Try:
(1) Comment out the int conversion operator for now (only to see what's wrong).
(2) Make the 2nd param of toString() const Convertable&.
Bziur
5th March 2014, 17:30
added: const to Convertable & - same problem
then removed int conversion:
main.cpp:8: error: invalid user-defined conversion from 'const Convertable' to 'const QString&' [-fpermissive]
return in1 + ": " + in2;
^
main.cpp:3: In file included from ..\Test\main.cpp:3:0:
convertable.h:19: candidate is: Convertable::operator QString() <near match>
convertable.h:19: note: no known conversion for implicit 'this' parameter from 'const Convertable*' to 'Convertable*'
main.cpp:8: error: passing 'const Convertable' as 'this' argument of 'Convertable::operator QString()' discards qualifiers [-fpermissive]
return in1 + ": " + in2;
^
Radek
5th March 2014, 17:50
Okay, try adding
operator const QString() const
{
return c_string;
}
Bziur
5th March 2014, 19:41
Sooo. It works.
But then again if I uncomment the int and add const to it. I get the same ambiguous overload warning
Added after 1 48 minutes:
So Lessons learned today
You shouldn't overload too many conversion functions as they lay confusion on c++ compilers when dealing with operators that accept multiple variable types. Stick to the necessary ones to avoid ambiguity and disarray.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.