-2
#include<iostream>
using namespace std;

class item{
    int a,b;
    void getdata(int x,int y){
        x=a;
        y=b;
    }
    public:
        void here(int a,int b)
        {
            void getdata();
        }
        void display();
};

void display()
{
     printf("a is %d",a);
     printf("\nb is %d ",b);
}
int main()
{
    item A;
    A.here(10,20);
    A.display();
}

Errors a and b are not declared in this scope for display function.

I cant also acces private function i.e void getdata(int,int) with a object without a public method, Right???

Raon
  • 113
  • 8
  • 1
    The display method definition is wrong. Should be `void item::display()`. Please put more effort into researching the problem before asking any questions. StackOverflow is NOT a forum. – thedemons Oct 21 '22 at 05:08
  • You only can access than inside the class and if you want to give them any value by passing parameters to the **here** function you should also send those parametres to the **getdata()** function too . – Mahdy.n Oct 21 '22 at 05:10
  • 1
    I see so many little mistakes here that I think what you need most is a bit more C++ knowledge. Work your way through chapter 2 (https://www.learncpp.com/cpp-tutorial/introduction-to-functions/). Have fun learning :) – Pepijn Kramer Oct 21 '22 at 05:16
  • If you're not planning to use C I/O, you should be including ``. Also, you should be asking why you aren't using C++ I/O. – Chris Oct 21 '22 at 05:50

1 Answers1

2

You've implemented a free function called display. The declared member function display does not have an implementation.

It should be:

void item::display() {
    std::printf("a is %d\n", a);
    std::printf("b is %d\n", b);
}

You should also #include <cstdio> to use std::printf.

Also note that the getdata function doesn't actually get any data since it assigns values to the local variables x and y. If you want the values available at the call-site, take x and y by reference:

void getdata(int& x, int& y) {  // note the & 
    x=a;
    y=b;
}

It also doesn't make sense to have getdata private. Instances of item can access the member variables directly so getdata is pointless unless made protected or public. You most probably want to have it public.

Since neither display nor getdata changes the object, you should also make them const qualified so that they can be used in const contexts.

The here function doesn't make sense since it calls getdata without arguments. I assume you want to assign values to the member variables a and b. If so:

void here(int a, int b) {
    this->a = a;
    this->b = b;
}

Demo

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • 1
    Good code review. But you shouldn't spend so much effort for absolute beginners. Let them learn first. For sure he would have identified all these errors alone. Then the learning effect would be much higher. – habrewning Oct 21 '22 at 05:37
  • 2
    @habrewning Perhaps. I'm not sure how much time OP spent on trying to figure out the mistakes. Sometimes people need a fresh pair of eyes to get the ball rolling again and since OP asked this question, I think that might be the case. – Ted Lyngmo Oct 21 '22 at 05:42
  • 1
    Pointing out a problem and it's solution is worth the time spent when it comes with a good explanation of why it was a problem and why the solution solved the problem. It may not be remembered as long as the stuff you learn from wasting time struggling through, but it's a hell of a lot more efficient. And the way I see it, Ted probably just eliminated at least one future Stack Overflow question that would have been downvoted and deleted. – user4581301 Oct 21 '22 at 05:54