簡體   English   中英

QT:當我有一個虛函數時,不能在沒有對象的情況下調用成員

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

我正在打電話,因為QT中的另一個功能

我的calendar.h虛擬函數:

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

我的calendarGregorian.h函數:

string whatDay(string)

還有我的函數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);

}

但是,當我編譯時..它顯示了這個錯誤:

錯誤:無法在沒有對象getDay = calendarGregorian :: whatDay(day)的情況下調用成員函數'虛擬std :: string calendarGregorian :: whatDay(std :: string)'; ^

請..我需要幫助

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;
    }
}

這樣,您不需要對象即可調用該函數。 這里的問題是方法需要調用對象,而靜態函數可以在沒有對象的情況下調用。

但是,如果您采用這種方式,請不要忘記您只能訪問靜態類變量,而不能訪問對象變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM