Cannot convert 'QJsonObject' to 'int' in return

443 Views Asked by At

So I have:

QJsonArray nodeCollection;

nodeCollection.push_back(ListElements(program, "title", "lang"));

And my ListElements method declares QJsonObject, fills it with things that I need, and should return the object so that it can be pushed to nodeCollection array variable:

ListElements (QDomElement root, QString tagname, QString attribute)
{

         auto nodeData = QJsonObject(
            {
            qMakePair(QString(itemElement.tagName()), QJsonValue(itemElement.text())),
            qMakePair(QString("lang"),QJsonValue(itemElement.attribute("lang"))),
            });

            return nodeData;  // <-- error
        }
    }
}

The error that I get is:

error: cannot convert 'QJsonObject' to 'int' in return

I'm obviously new to C++ and I've googled this issue with some success, but no concrete examples how to address this issue.

What am I doing wrong? How can I tell the method it should return QJsonObject instead of int?

2

There are 2 best solutions below

0
On BEST ANSWER

You have to add the return type to the beginning of the function prototype:

QJsonObject ListElements (QDomElement root, QString tagname, QString attribute)

If you don't specify a return type, the default assumed return type is int, that's why you got this error.

0
On

If you write a function like this:

MyFunc(double, float) {
   ...
}

Then it is assumed to have a return type of int because no return type has been specified. If you want it to return something else you need to specify the return type:

QJsonObject MyFunc(double, float) {
   ...
}