user17629
user17629

Reputation: 169

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

Upvotes: 1

Views: 1099

Answers (1)

Theolodis
Theolodis

Reputation: 5092

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.

Upvotes: 1

Related Questions