[英]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.