简体   繁体   English

Qt C ++无法在没有对象的情况下调用成员函数“”

[英]Qt C++ cannot call member function ' ' without object

I keep getting this error: 我不断收到此错误:

cannot call member function 'QString Load::loadRoundsPlayed()'without object

Now im pretty new to c++ and qt so im not sure what this means. 现在,我对c ++和qt还是很陌生,所以我不确定这意味着什么。 I am trying to call a function from another class to set the number on some lcdNumbers. 我试图从另一个类调用一个函数来设置一些lcdNumbers上的数字。 Here is the Load.cpp which holds the function: 这是包含函数的Load.cpp:

#include "load.h"
#include <QtCore>
#include <QFile>
#include <QDebug>

Load::Load() //here and down
{}

QString Load::loadRoundsPlayed()
{
    QFile roundsFile(":/StartupFiles/average_rounds.dat");

    if(!roundsFile.open(QFile::ReadOnly | QFile::Text))
    {
        qDebug("Could not open average_rounds for reading");
    }

    Load::roundsPlayed = roundsFile.readAll();
    roundsFile.close();
    return Load::roundsPlayed;
}

And here is the Load.h: 这是Load.h:

    #ifndef LOAD_H
     #define LOAD_H

    #include <QtCore>

    class Load
    {
    private:
        QString roundsPlayed; //and here
    public:
        Load();
        QString loadRoundsPlayed(); //and here
    };

    #endif // LOAD_H

And finally the place where i call the function: 最后是我调用该函数的地方:

    #include "mainwindow.h"
     #include "ui_mainwindow.h"
    #include "load.h"
    #include <QLCDNumber>

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        MainWindow::startupLoad();
    }

    MainWindow::~MainWindow()
    {
        delete ui;
    }
    void MainWindow::startupLoad()
    {
        ui->roundPlayer_lcdNumber->display(Load::loadRoundsPlayed()); //right here
    }

When i run this i get that error. 当我运行这个我得到那个错误。 Im not sure what it means so if anyone could help i would be thankfull. 我不确定这是什么意思,所以如果有人可以帮助我,我将非常感激。 Thanks. 谢谢。

The error description is pretty clear 错误说明很清楚

cannot call member function 'QString Load::loadRoundsPlayed()'without object 没有对象就无法调用成员函数“ QString Load :: loadRoundsPlayed()”

You cannot call member functions, that are not static, without creating instance of the class. 如果不创建类的实例,则不能调用非静态的成员函数。


Looking at you code, you probably need to do this: 查看您的代码,您可能需要执行以下操作:

Load load;
ui->roundPlayer_lcdNumber->display(load.loadRoundsPlayed()); //right here

There are two other options: 还有其他两种选择:

  • make loadRoundsPlayed static and roundsPlayed static, if you don't want them to be associated with the concrete instances OR 如果不希望将它们与具体实例相关联,请使loadRoundsPlayed静态和roundsPlayed静态,或者
  • make loadRoundsPlayed static and return QString by copy, that will be locally created inside the function. 使loadRoundsPlayed静态并通过复制返回QString ,它将在函数内部本地创建。 Something like 就像是

:

QString Load::loadRoundsPlayed()
{
    QFile roundsFile(":/StartupFiles/average_rounds.dat");

    if(!roundsFile.open(QFile::ReadOnly | QFile::Text))
    {
        qDebug("Could not open average_rounds for reading");
    }

    QString lRoundsPlayed = roundsFile.readAll();
    roundsFile.close();
    return lRoundsPlayed;
}

Because the method and member are not associated with class instances, make it static: 因为方法和成员不与类实例相关联,所以使其静态:

class Load
{
private:
    static QString roundsPlayed;
public:
    Load();
    static QString loadRoundsPlayed();
};

If you want them to be associated with instances, you'll need to create an object and call the method on it (it doesn't have to be static in this case). 如果希望它们与实例相关联,则需要创建一个对象并对其调用方法(在这种情况下,它不必是static )。

In Load::loadRoundsPlayed() , you should change Load::loadRoundsPlayed() ,您应该更改

Load::roundsPlayed = roundsFile.readAll();

to

this->roundsPlayed = roundsFile.readAll();  

or simply 或简单地

roundsPlayed = roundsFile.readAll();

This particular example won't fix the compiler error, but it illustrates where you are having some confusion with the syntax. 这个特定的示例无法解决编译器错误,但是它说明了您对语法有些困惑。 When you prefix a function or variable name with "Load::", you are saying that you want the field that belongs to this class. 当给函数或变量名加上“ Load ::”前缀时,就是说您要属于该类的字段。 However, every object of a class will have its own copy of the variables that you have declared in it. 但是,类的每个对象都将拥有您在其中声明的变量的副本。 This means that you need to create an object before you can use them. 这意味着您需要先创建一个对象,然后才能使用它们。 Similarly, functions are bound to objects, so you again need an object in order to call a member function. 同样,函数绑定到对象,因此您再次需要一个对象才能调用成员函数。

The other option is to make your functions static so that you don't need an object to call it. 另一种选择是使函数static ,这样就不需要对象来调用它。 I strongly encourage you to learn about the difference between instance functions and static functions of a class so that you can use these two tools appropriately when the situation calls for it. 我强烈建议您了解类的实例函数和静态函数之间的区别,以便在情况需要时可以适当地使用这两个工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM