C++ QT:当我有一个虚拟函数时,不能调用没有对象的成员
我正在调用QT中的另一个函数 calendar.h中的我的虚拟函数:C++ QT:当我有一个虚拟函数时,不能调用没有对象的成员,c++,qt,C++,Qt,我正在调用QT中的另一个函数 calendar.h中的我的虚拟函数: virtual string whatDay(string){ return "";} static string whatDay(string){ return "";} calendarGregorian.h中的我的函数: string whatDay(string) class CalendarGregorian: Calendar{ public: static int superCalculationF
virtual string whatDay(string){ return "";}
static string whatDay(string){ return "";}
calendarGregorian.h中的我的函数:
string whatDay(string)
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;
}
}
和mainwindow.cpp中单击的函数
void MainWindow::on_whatDayButton_clicked()
{
QString whatDayString;
string getDay;
whatDayString = ui->lineGetDay->text();
string day = whatDayString.toUtf8().constData();
getDay = calendarGregorian::whatDay(day);
}
但是,当我编译的时候。。它显示了以下错误:
错误:无法在没有对象的情况下调用成员函数“virtual std::string calendarGregorian::whatDay(std::string)”
getDay=日历公历::whatDay(天);
^
请。。我需要帮助日历。h:
virtual string whatDay(string){ return "";}
static string whatDay(string){ return "";}
日历公历h:
string whatDay(string)
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;
}
}
这样,您就不需要对象来调用函数。这里的问题是,方法需要调用对象,而静态函数可以在没有对象的情况下调用
但是如果您这样做,不要忘记您只能访问静态类变量,而不能访问对象变量。您还不明白什么?成员函数需要调用一个对象(例如,
某个日历。什么日子(day)
)。您是否可能混淆了虚拟
和静态
?前者用于子类化,后者用于调用不需要对象的成员函数。我直接从我的类calendarGregorian调用该函数,我没有创建任何对象。我已经在尝试使用静态。。同样的问题如果你是从类中调用函数,你可能应该使用getDay=this->calendargorian::whatDay(day)代码>。但是你给我们展示的函数在类main窗口中,而不是calendar
或calendargorian
我已经尝试过了,但没有成功,我的函数是:@user17629,当你将它们都设置为静态时,你会得到什么错误,calendargorian::whatDay()
和calendar::whatDay())
?