c++qtqjson

Cannot convert 'QJsonObject' to 'int' in return


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?


Solution

  • 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.