简体   繁体   English

QT:当我有一个虚函数时,不能在没有对象的情况下调用成员

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

I'm calling since another function in QT 我正在打电话,因为QT中的另一个功能

My virtual function from calendar.h: 我的calendar.h虚拟函数:

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

My function from calendarGregorian.h: 我的calendarGregorian.h函数:

string whatDay(string)

And my function on_whatDayButton_clicked() from mainwindow.cpp 还有我的函数on_whatDayButton_clicked()来自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); 错误:无法在没有对象getDay = calendarGregorian :: whatDay(day)的情况下调用成员函数'虚拟std :: string calendarGregorian :: whatDay(std :: string)'; ^ ^

Please.. I need help 请..我需要帮助

calendar.h: calendar.h:

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

calendarGregorian.h: 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. 但是,如果您采用这种方式,请不要忘记您只能访问静态类变量,而不能访问对象变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:没有 object 无法呼叫成员 function - 但我有一个 object? - error: cannot call member function without object - but I have an object? 没有对象就无法调用成员函数“ virtual void ThreadBase :: doTask()” - cannot call member function 'virtual void ThreadBase::doTask()' without object QT,C ++无法在没有对象的情况下调用成员函数 - QT, C++, cannot call member function without object Qt C ++无法在没有对象的情况下调用成员函数“” - Qt C++ cannot call member function ' ' without object 没有对象就无法调用成员函数,但是我有一个对象才能调用该函数 - Cannot call member function without object but I call the function with an object 试图在QLabel上绘画失败(无法在没有对象的情况下调用成员函数'virtual void QLabel :: paintEvent(QPaintEvent *)') - Trying to paint on a QLabel fails (cannot call member function 'virtual void QLabel::paintEvent(QPaintEvent*)' without object) 我的Qt代码无法编译“无法调用没有对象的成员函数” - My Qt code doesn't compile with “cannot call member function without object” 不能调用没有 object 的成员 function - cannot call member function without object 命名空间:不能在没有对象的情况下调用成员函数 - namespace: cannot call member function without object 没有对象错误就无法调用成员函数 - cannot call member function without object error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM