![](/img/trans.png)
[英]error: cannot call member function without object - but I have an object?
[英]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.