标签: static-methods

错误“无效使用非静态成员函数”:结果取决于编译器/操作系统?

使用下面的 MCVE,当我classes_test.cc使用以下命令编译源代码时

\n
$ c++ --version\nc++.exe (Rev3, Built by MSYS2 project) 10.2.0\n$ c++.exe -Wall -Wunused -Wuninitialized -g -o classes_test classes_test.cc -std=c++17\n
Run Code Online (Sandbox Code Playgroud)\n

在 Windows 10 + Msys2 下(没有选项-std=c++17我得到相同的结果),我得到了预期的结果

\n
$ ./classes_test.exe\nUsing GNU G: True\nPrototype of BaseClass::get_bc() is int (BaseClass::*)()\n
Run Code Online (Sandbox Code Playgroud)\n

当我在 Ubuntu 下的 g++ 中尝试相同的操作时,出现编译错误

\n
$ c++ --version\nc++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0\n$ c++ -Wall -Wunused -Wuninitialized -g -o classes_test classes_test.cc -std=c++17\nclasses_test.cc: In function \xe2\x80\x98int main()\xe2\x80\x99:\nclasses_test.cc:63:83: error: invalid use of non-static member function \xe2\x80\x98int BaseClass::get_bc()\xe2\x80\x99\n  63  | …
Run Code Online (Sandbox Code Playgroud)

c++ linux static-methods msys2

0
推荐指数
1
解决办法
74
查看次数

有没有办法将函数标记为只能从静态构造函数调用/可调用?

我的一个类有一个相当大的静态构造函数,并且想要重构它以将初始化的各个部分封装在静态函数中。

One of the things this static constructor does a lot of is initialize the values of static readonly fields. However, when I try to move these parts into functions, obviously the compiler wont let me as these can only be set in the static constructor. This makes sense as it doesn't know that I only intend to call these functions from my static constructor.

Is there any way around this? e.g. is there some sort of attribute I can …

c# static-methods static-constructor readonly-variable

0
推荐指数
1
解决办法
441
查看次数

我想了解抽象类应用程序的 launch() 方法在 JavaFX 中如何工作

在javaFX中Application是一个抽象类。在这个抽象类中,有一些抽象方法在扩展抽象应用程序类的主类中被重写,并且在抽象类应用程序中也有静态 launch() 方法。launch() 方法从主类中的 main 方法调用。现在 launch() 方法如何调用这些抽象方法,并且对于这些调用,执行主类中的重写方法?请帮助我了解这个程序实际上是有效的。我知道非静态方法不能从静态方法调用,并且不可能创建抽象类的实例。抽象方法不能是静态方法。

我不想创建主类的对象。因为launch()方法不知道主类的名称是什么。

给我写一段java代码,这个过程很好地说明了这一点。像这样

public class main extends Application{
    public static void main(String[] args)
        launch();                       // launch() was a static method in Application class
    }
    @override
    public void init(){                 // init() was an abstract method in Application class
        System.out.println("hello");
    }
}

public abstract class Application {
    public static void launch(){
        init();
    }
    public abstract void init();
}
Run Code Online (Sandbox Code Playgroud)

我想得到输出:你好

java abstract-class static-methods javafx abstract-methods

0
推荐指数
2
解决办法
242
查看次数

PHP语法错误:第78行意外的T_PAAMAYIM_NEKUDOTAYIM

我检查了所有可能的答案,但很抱歉,我无法理解这一点.我尝试使属性非静态但代码键显示行上的错误:

$task = new sfCacheClearTask($sfContext::getInstance()->getEventDispatcher(), new sfFormatter());
Run Code Online (Sandbox Code Playgroud)

请参阅http://codepad.org/zcqrkVoY

Pl帮助解决这个问题.谢谢

class doSokiJobTask extends sfBaseTask {

    CONST STATUS_PENDING = 0;
    CONST STATUS_DONE = 1;
    CONST STATUS_IN_PROGRESS = 2;
    CONST STATUS_FAILED = 3;
    CONST STATUS_CANCELLED = 4;

    CONST TIME_TO_DIE = 60;

    public $context = null;
    public $job_name = null;
    public $done = null;
    public $status = null;
    public $start = 0;

    protected function setJobName($name) {
        $this->job_name = strtolower($name);
    }

    protected function configure() {
        $this->addArguments(array(
                //new sfCommandArgument('build', sfCommandArgument::REQUIRED, ''),
        ));

        $this->addOptions(array(
            new sfCommandOption('env', …
Run Code Online (Sandbox Code Playgroud)

php static static-methods php-5.2

-1
推荐指数
1
解决办法
1238
查看次数

无法调用静态函数

我在标题中声明了traductionCSV.h这个函数

static QVector<struct variableDurSupervision>
    listVariableDurSupervison(std::string fichierCSV);
Run Code Online (Sandbox Code Playgroud)

我在我的cpp中写它,然后我想在另一个文件中使用它supervision.cpp,所以我这样称呼它:

remplirDurCellule(
    traductionCSV::listVariableDurSupervison(
        "../../FichierCSV/ListeVariableSupervision.csv"
    )
);
Run Code Online (Sandbox Code Playgroud)

但它不起作用,我得到了这个错误:

对traductionCSV :: listVariableDurSupervison(std :: string)的未定义引用

我正确包含了所有文件,所以我不明白.

谢谢.

c++ qt static-methods

-1
推荐指数
1
解决办法
88
查看次数

其他方法在静态方法中看不到

我正在尝试使用LINQ to Colletion但问题是方法ShowLINQ()无法在方法中看到Main().

class Program
{
    static void Main(string[] args)
    {
        ShowLINQ();//Error line
    }

    public void ShowLINQ()
    {
        List<Element> elements = BuildList();
        var query = from d in elements
                    where d.AtomicNumber < 22
                    orderby d.Name
                    select d;

        foreach (Element d in query)
        {
            Console.WriteLine(d.Name + " " + d.AtomicNumber);
        }
    }
    Snippet...
}
Run Code Online (Sandbox Code Playgroud)

c# static-methods console-application

-1
推荐指数
1
解决办法
423
查看次数

非静态方法getActivity()

我使方法"showResult"静态但我有问题:

"Toast.makeText(`getActivity()`.getApplication(), result2 + "\n" + "Total Amount:=" + totalAmount2 + "€", Toast.LENGTH_LONG).show();"
Run Code Online (Sandbox Code Playgroud)

怎么解决我的问题?我将分享我的代码.

感谢每一个人!

这是我的第一个类Fragment.java

public class MyListFragment extends Fragment implements
        android.widget.CompoundButton.OnCheckedChangeListener {

    ListView lv;
    ArrayList<Planet> planetList;
    static PlanetAdapter plAdapter;
    BirraAdapter biAdapter;
    PlanetAdapter.PlanetHolder holder;




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);

        Button mButton = (Button) rootView.findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showResult(v);


            }
        });
        //return inflater.inflate(R.layout.fragment_list2, container, false);
        return …
Run Code Online (Sandbox Code Playgroud)

android static-methods android-fragments android-studio

-1
推荐指数
1
解决办法
3525
查看次数

为什么在Python中根据@staticmethod选择模块级别的函数(根据Google样式指南)?

根据《 Google Python样式指南》,绝对不应(几乎)使用静态方法:

除非为了与现有库中定义的API集成而强制使用,否则切勿使用@staticmethod。改为编写模块级函数

该建议背后的原因是什么?

这仅适用于Google吗?还是在Python中使用静态方法还有其他(更一般的)缺点?

尤其是,如果我想在将由该类的其他公共成员函数调用的类内实现实用程序功能,则最佳实践是什么?

class Foo: 
    .......
    def member_func(self): 
        some_utility_function(self.member)
Run Code Online (Sandbox Code Playgroud)

Google Python样式指南

python static-methods

-1
推荐指数
2
解决办法
437
查看次数

C++:如何使用静态函数定义类

我想创建一个C++类,它只与静态函数合并,无论如何都可以使用.我创建了一个.h包含声明的.cpp文件和一个包含定义的文件.但是,当我在我的代码中使用它时,我收到一些奇怪的错误消息,我不知道如何解决.

这是我的Utils.h文件的内容:

#include <iostream>
#include <fstream>
#include <sstream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

#include <vector>
#include <opencv/cv.h>
#include <opencv/cxcore.h>

class Utils
{
public:
    static void drawPoint(Mat &img, int R, int G, int B, int x, int y);
};
Run Code Online (Sandbox Code Playgroud)

这是我的Utils.cpp文件的内容:

#include "Utils.h"

void Utils::drawPoint(Mat &img, int R, int G, int B, int x, int y)
{
img.at<Vec3b>(x, y)[0] = R;
img.at<Vec3b>(x, y)[1] = G;
img.at<Vec3b>(x, y)[2] = B; …
Run Code Online (Sandbox Code Playgroud)

c++ opencv static-methods

-2
推荐指数
1
解决办法
669
查看次数

这个Singleton类有什么问题吗?

在这些条件下,我编写了下一个Singleton类:
1 - 我想要一个且只有一个类的实例存在并且可以从整个游戏引擎访问.
2 - Singleton被密集使用(每帧数千次)所以我不想写一个额外的GetInstance()函数,我试图避免任何额外的函数调用性能
3 - 一种可能性是让GetInstance()像内联一样这个 :

inline Singleton* Singleton::GetInstance()
{
  static Singleton * singleton = new Singleton();
  return singleton;
}
Run Code Online (Sandbox Code Playgroud)

但这会引起一个引用问题,每次调用时都会有一个对单例的新引用,用于修复用c ++编写的内容:

class Singleton{
private:
    static   Singleton* singleton;
    Singleton(){}

public:
    static inline  Singleton* GetInstance() // now can be inlined !
    {
        return singleton;
    }

    static void Init()
    {
        // ofc i have to check first if  this function
        // is active only once
        if(singleton != nullptr)
        {
            delete singleton;
        }

        singleton = new Singleton();
    } …
Run Code Online (Sandbox Code Playgroud)

c++ singleton implementation static-methods

-2
推荐指数
2
解决办法
641
查看次数