QString conversion function
Hello. I'm trying to make my work easier by overloading QString() conversion function:
Code:
private:
public:
if( c_type == STRING )
return *c_string;
else
return toString();
}
then I'm trying to use it like this
Code:
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?
Re: QString conversion function
We currently cannot see enough. Please post a small, complete example that fails to compile in this way.
Re: QString conversion function
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
Code:
#ifndef CONVERTABLE_H
#define CONVERTABLE_H
#include <QString>
class Convertable
{
private:
int i;
public:
Convertable
(int in
=0,
const QString string
= QString()):c_string
(string
),i
(in
) {}
operator int()
{
return i;
}
{
return c_string;
}
};
#endif // CONVERTABLE_H
main.cpp
Code:
#include <QCoreApplication>
#include "convertable.h"
#include <QDebug>
{
return in1 + ": " + in2;
}
int main(int argc, char *argv[])
{
Convertable B("something");
qDebug() << toString(A,B);
return a.exec();
}
Re: QString conversion function
Please specify what all is wrong now. At least, the following will not pass:
Code:
Convertable B("something");
You do not have a ctor for it. Try
Code:
Convertable B(0,"something");
Re: QString conversion function
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')
Code:
return in1 + ": " + in2;
^
Re: QString conversion function
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&.
Re: QString conversion function
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]
Code:
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]
Code:
return in1 + ": " + in2;
^
Re: QString conversion function
Okay, try adding
Code:
{
return c_string;
}
Re: QString conversion function
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.