简体   繁体   中英

QT : cannot call member without object when I have a virtual function

I'm calling since another function in QT

My virtual function from calendar.h:

virtual string whatDay(string){ return "";}

My function from calendarGregorian.h:

string whatDay(string)

And my function on_whatDayButton_clicked() from mainwindow.cpp

void MainWindow::on_whatDayButton_clicked()
{
    QString whatDayString;
    string getDay;
    whatDayString = ui->lineGetDay->text();
    string day = whatDayString.toUtf8().constData();
    getDay = calendarGregorian::whatDay(day);

}

But, when I'm compiling.. it show me this error:

error: cannot call member function 'virtual std::string calendarGregorian::whatDay(std::string)' without object getDay = calendarGregorian::whatDay(day); ^

Please.. I need help

calendar.h:

static string whatDay(string){ return "";}

calendarGregorian.h:

class CalendarGregorian: Calendar{
public:
    static int superCalculationFactor = 276485;
    int notSoGood;
    static string whatDay(string)
    {
        //do the formatting using superCalculationFactor
        //you can't use notSoGood!
        return result;
    }
}

that way you don't need an object to call the function. The problem here was that methods need objects to be called on while static functions can be called without an object.

But if you go this way, don't forget that you have only access to static class variables, and not at object variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM