简体   繁体   English

编译器错误:“非静态成员引用必须相对于特定对象”

[英]Compiler error: “a nonstatic member reference must be relative to a specific object”

I've been trying to learn how to declare functions inside a class in CPP:我一直在尝试学习如何在 CPP 中的 class 中声明函数:

This is the program I wrote:这是我写的程序:

    //Passing object as function arguments and returning objects
#include <iostream>
using namespace std;
class item{ //Function can be declared either within the class (in that case, there is no need of making the data private)
    int price;
    float cost;
    public:
    int newFunction(item a){
        a.cost=10;
        a.price=40;
        return a.cost;
    }
} obj1;
int main(){
    cout << item::newFunction(obj1);
    return 0;
}

Can someone tell why does the compiler give error "a nonstatic member reference must be relative to a specific object".有人可以告诉编译器为什么会给出错误“非静态成员引用必须相对于特定对象”。

Also, can someone tell the difference between:: (scope resolution operator) and accessing class elements using.(dot operator).另外,有人可以区分::(范围解析运算符)和使用.(点运算符)访问 class 元素之间的区别。 I am sort of confused between the two, and Googling the difference didn't bring any matchable results.我在两者之间有点困惑,谷歌搜索差异并没有带来任何可匹配的结果。

Heres your answer:-这是你的答案:-

Error Message:错误信息:

14:35: error: cannot call member function 'int item::newFunction(item)' without object

You can't just call a class function without making any object of it because you cannot directly access a class function if its not created in RAM first, thats why we create a object of a class. You can't just call a class function without making any object of it because you cannot directly access a class function if its not created in RAM first, thats why we create a object of a class.

I made a temporary object in main function named obj2 and it works.我在名为 obj2 的主 function 中制作了一个临时 object 并且它可以工作。

Heres the added line这是添加的行

#include <iostream>
using namespace std;

class Item { //Function can be declared either within the class (in that case, there is no need of making the data private)
    int price;
    float cost;

public:

    int newFunction(item a) {
        a.cost=10;
        a.price=40;
        return a.cost;
    }

} obj1;
int main() {
    Item obj2;
    cout << obj2.newFunction(obj1);
    return 0;
}

Output: Output:

10

Also its not a good practice to not use camelcase in naming a class, so do keep that in mind:)此外,在命名 class 时不使用驼峰式命名也不是一个好习惯,所以请记住这一点:)

Heres your answer What is the difference between "::" "."这是你的答案“::” 和“。”有什么区别? and "->" in c++ 和 c++ 中的“->”

cout << item::newFunction(obj1); This only works when you declare the newFunction() as static .这仅在您将newFunction()声明为static时有效。 Otherwise, if you would like to call the member function, you have to create an object.否则,如果你想调用成员 function,你必须创建一个 object。 item Obj; And then call Obj.newFunction(obj1);然后调用Obj.newFunction(obj1);

暂无
暂无

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

相关问题 非静态成员引用必须相对于特定对象? - A nonstatic member reference must be relative to a specific object? 非静态成员引用必须相对于特定的 object - A nonstatic member reference must be relative to a specific object 错误:非静态成员引用必须相对于特定对象 - error:a nonstatic member reference must be relative to a specific object C ++错误非静态成员引用必须相对于特定对象 - C++ error A nonstatic member reference must be relative to a specific object 当两个成员都在同一类中时,出现错误“非静态成员引用必须相对于特定对象” - Getting error “a nonstatic member reference must be relative to a specific object” while both member are in the same class 非静态成员引用必须相对于特定对象(父类问题) - a nonstatic member reference must be relative to a specific object (parent class problems) C ++非静态成员引用必须相对于特定对象 - C++ A nonstatic member reference must be relative to a specific object C ++非静态成员引用必须相对于特定对象 - C++ nonstatic member reference must be relative to a specific object C++:非静态成员引用必须相对于特定对象 - C++: a nonstatic member reference must be relative to a specific object 一个新的新类对象的功能; 错误:非静态成员引用必须相对于特定对象 - The function of a new new class object; error: A nonstatic member reference must be relative to a specific object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM