1

I'm calling since another function in QT

My virtual function from calendar.h:

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

My function from calendarGregorian.h:

string whatDay(string)

And my function on_whatDayButton_clicked() from 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); ^

Please.. I need help

8
  • 2
    What don't you understand? Member functions need an object to be called on (e.g. some_calendar.whatDay(day)). Commented Apr 22, 2014 at 7:25
  • 3
    Is it possible you are confusing virtual and static? The former is needed for subclassing and the latter to call member functions who do not require an object. Commented Apr 22, 2014 at 7:27
  • I'm calling the function directly from my class calendarGregorian, I don't create any object some_calendar Commented Apr 22, 2014 at 7:27
  • I already trying with static.. the same problem Commented Apr 22, 2014 at 7:28
  • If you are calling the function from within the class you should probably use getDay = this->calendarGregorian::whatDay(day);. But the function you showed us is in the class MainWindow and not calendar or calendarGregorian Commented Apr 22, 2014 at 7:29

1 Answer 1

1

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

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I already tried that and I don't have success and my function it's: ur1.ca/h566i
@user17629 and what error do you get when you make them both static, calendarGregorian::whatDay() and calendar::whatDay()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.