Click here to Skip to main content
14,975,411 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the attached C++ code snippet I get


QString::toStdString(__FUNCTION__ ) , but

QString::number(__LINE__ )


After reading the other forum I still have no idea what "object" to add, why and how to the QString::toStdString(__FUNCTION__ )

<a href="">c++ - Call to non-static member function without an object argument compiler error - Stack Overflow</a>[<a href="" target="_blank" title="New Window">^</a>]

ui->textEdit_2->append("TASK  END setup all connect " + QString::toStdString(__FUNCTION__ ) + " @ line " + QString::number(__LINE__ ));

What I have tried:

Read the other forum solution to the issue.
Updated 1-May-21 6:11am
Aghast (nj) 1-May-21 16:57pm
I think you may be making this more complex than it needs to be. The preprocessor's __LINE__ symbol is treated as an object-like macro. So you can stringify it in the preprocessor:

#define STR(x) #x
#define LINE_STR STR(__LINE__) // Note: there must be 2 macros.

On the other hand, __func__ and __FUNCTION__ and friends were once treated as object-like macros defined to literals, but now are treated as variables. I don't know why. So you can't just stringify it, or juxtapose it like you could with __FILE__. :(

QString does have a QString::fromUtf8() and a ::fromLocal8bit() class method. If your file names are in ascii, the utf-8 will work (since ascii is a subset of utf-8). If your file names are in a different encoding, you might need to write some more complex code.

Finally, note that the QString class defines functions called "from xxx" for creating a QString from external data, and defines methods called "to xxx" for extracting data from an already-existing QString. Doing what you are doing, you should look for either a QString() constructor or a QString::from... function to convert.

The function is non-static so it needs to be called on an existing QString object:
QString foo = "something";
std::string bar = foo.toStdString();
CPallini 1-May-21 16:02pm
There are two types of member functions: static and nonstatic.
Static functions can be called by prefixing them with the class name:
And are not associated with any instance of the MyClass class - the function cannot access anything (variables, properties, other functions) that are not also declared as static

Nonstatic functions cannot be called via the class name because they are intended to do work on one particular class instance, so they have full access to all the nonstatic variables and so forth that the class contains. This is the default - if you do not specific the static keyword, you create a nonstatic function. You call a nonstatic by using the name of the variable that contains the instance:
MyClass mc = new MyClass();
CPallini 1-May-21 16:01pm
Member 14968771 3-May-21 12:55pm
Perhaps I am getting confused with static variables.
Anyway I definitely created monster with passing all these non QString to a function who requires QString. And that is only for debugging anyway.
Here is my latest hack. It looks awful, but works.

The key is that append function expects QString as parameter and I need to have that parameter in "piece parts" and some pieces are variable.

<pre>                                       ui->textEdit_2->append(QString ( "Function " ) +__FUNCTION__ + QString("TEST  @ line ") + QString(QString::number(__LINE__ )));
                                         ui->textEdit_2->append(QString ( "Q_FUNC_INFO " ) +Q_FUNC_INFO + QString("TEST  @ line ") + QString(QString::number(__LINE__ )));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900