错误:非静态成员引用必须相对于特定对象 [英] Error : a nonstatic member reference must be relative to a specific object

查看:135
本文介绍了错误:非静态成员引用必须相对于特定对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class myClass
{
public:
   static int sFunc();
   int iAdd(int a, int b);
};

int myClass::sFunc()
{
   return iAdd(124,142); // ERROR apears here ; a nonstatic member reference must be relative to a specific object
}







Why is that and how to prevent the error ?

推荐答案

类内的静态函数与类外的静态函数基本相同:
A static function inside a class is basically the same as a function outside the class:
class myClass
{
public:
   int iAdd(int a, int b);
};

int sFunc()
{
   return iAdd(124,142);
}


很奇怪,不是吗?

静态方法和类外部函数之间的唯一区别是,静态方法可以看到类的私有成员,而外部函数看不到,而类外部的代码可以访问外部函数,而静态方法可以被置于保护状态或私人状态,使其无法对大多数外部世界隐瞒.

在静态方法内部,您甚至可以到达指定类的私有成员,但是您必须首先以某种方式将该类的实例传递给该函数(因为静态方法类似于普通函数,而没有this指针指向类实例).例如,如果将myClass的实例实例化为全局变量,则您的静态方法可以访问它,或者可以通过将myClass实例作为参数传递给您的静态方法来模拟this指针,或者您可以仅为像迈耶的单例模式一样将自己置于静态方法中:


Pretty weird, isn''t it?

The only differences between a static method and a function outside the class is that a static method can see the private members of the class while the outer function doesn''t, and code outside the class can access the outer function while the static method might be put to protected or private hiding it from most of the outer world.

Inside a static method you can reach even private members of the specified class but you have to pass somehow an instance of that class to that function first (because static methods are like normal functions, without a this pointer to a class instance). For example if you instantiate an instance of myClass as a global variable then your static method can access it, or you can simulate the this pointer by passing in a myClass instance as a parameter to your static method, or you can just create an instance for yourself inside the static method like the meyer''s singleton pattern:

class myClass
{
public:
    // meyers singleton, works fine
    static myClass& GetInstance()
    {
        static myClass g_Instance;
        return g_Intance;
    }
private:
    myClass() {}
};

// This wouldn't work because this GetInstance() doesn't see the private constructor of the class.
myClass& GetInstance()
{
    static myClass g_Instance;
    return g_Intance;
}


iAdd方法也必须是静态的.
您不能从静态方法调用非静态方法.
我建议您读一本C ++书,以稍微了解一下静态方法和实例方法之间的区别.在使用它的同时,您将必须了解实例是什么.

myClass a;

-> a是myClass的实例
-> a.iAdd(5,5);可以,但是如果返回10,则使其成为普通成员没有任何意义,因为它不使用myClass类的任何内容.因此,如果iAdd(int a,int b)返回a + b; ,那么将其设为静态是正确的.

但是您已经读了一些基本的东西.
the method iAdd must be static too.
You cannot call a non static method from a static method.
I suggest you read a C++ book to read a little bit on what the difference is between a static method and an instance method. While you are at it, you will have to learn what an instance is.

myClass a;

-> a is an instance of myClass
-> a.iAdd(5,5); would work, but if it returns 10, then there is no point in making it a normal member because it does not use anything of the myClass class. so if iAdd(int a, int b) is return a+b; , then it is correct to make it static.

But you have some reading to do this is all pretty basic stuff.


sFunc是静态的,因此与该类的任何特定实例都不相关.
iAdd不是静态的,因此它需要类的实例才能被调用.

考虑以下代码:
sFunc is static, so is not related to any particular instance of the class.
iAdd is not static, so it requires an instance of the class in order to be called.

Consider the following code:
myClass::sFunc();



这通常是有效的,但是,在没有类实例的情况下,sFunc()如何调用iAdd()呢?答案是;它不能.这就是为什么出现错误的原因.
希望有帮助.



This would normaly be valid, but think, how is sFunc() going to call iAdd() without an instance of the class? Answer is; it can''t. And that is why you get the error.

Hope that helps.


这篇关于错误:非静态成员引用必须相对于特定对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆