-3

I've been trying to learn how to declare functions inside a class in CPP:

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). I am sort of confused between the two, and Googling the difference didn't bring any matchable results.

  • 2
    [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – 273K Mar 10 '21 at 14:59
  • 1
    Also the signature is odd. Having a member function that doesn't use the objects own data, but works on an unrelated object defies the point of having member functions in the first place. A good C++ book should explain this too. – Lukas-T Mar 10 '21 at 15:01
  • @S.M. Can you tell the error in my code as well? – programmingEnthusiast Mar 10 '21 at 15:02
  • @churill What do you mean by signature? – programmingEnthusiast Mar 10 '21 at 15:03
  • Instead of writing `newFunction(item a)`, you could have written `newFunction()` and acted directly on `price` and `cost`. Then called it using `obj1.newFunction()` instead of `item::newFunction(obj1)`. But honestly at this point you're asking several different very fundamental questions and the number of people replying with "Please read a book" will grow. – Nathan Pierson Mar 10 '21 at 15:04

2 Answers2

1

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.

I made a temporary object in main function named obj2 and it works.

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:

10

Also its not a good practice to not use camelcase in naming a class, so do keep that in mind :)

Heres your answer What is the difference between "::" "." and "->" in c++

Sad-Poyo
  • 11
  • 2
1

cout << item::newFunction(obj1);This only works when you declare the newFunction() as static. Otherwise, if you would like to call the member function, you have to create an object. item Obj; And then call Obj.newFunction(obj1);

jiadong
  • 135
  • 7