0

I got stuck with creating a non-friend global function that has one parameter with pointer. In simple words I need a function that get passesd object via pointer, and write out all its members. So far I created a public declaration in MyClass

   void WriteOutMyClass2 (MyClass *kp2);

And I created the function as well

void WriteOutMyClass2 (MyClass *kp2)
{

    cout << "_a :" <<kp2._a <<endl;

}

but unfortunately I'm getting an error:

request for member ‘_a’ in ‘kp2’, which is of pointer type ‘MyClass*’ (maybe you meant to use ‘->’ ?)

I've pasted code at rexter.com, so you can look at that here. http://rextester.com/URTI56137

And additionally I'm attaching the whole code here.

class MyClass
{
   private:
   int _a;
   int* c;
   int size;
   static int counter;

   public:
   friend void WriteOutMyClass1 (MyClass &kp1);
   void WriteOutMyClass2 (MyClass *kp2);
   MyClass() //default constructor
   {
       _a=0;
       size = 10;
       c = new int [size];
       for(int i = 0; i<size; i++)
       {
           c[i] = 1;
       }
       counter++;
   }



};


//Initialize friend fucntion
void WriteOutMyClass1 (MyClass &kp1)
{

    cout << "_a :" <<kp1._a <<endl;
    cout << "size :" <<kp1.size<<endl;
    cout << "counter :" <<kp1.counter<<endl;
        for(int i = 0; i<kp1.size; i++)
       {
            cout << "c[" << i << "] = "  << kp1.c[i] << endl;
       }
}

//Initialize non-friend global fucntion.
void WriteOutMyClass2 (MyClass *kp2)
{

    cout << "_a :" <<kp2._a <<endl; // here I'm getting error.

}
int main()
{


}

edit:

What excatly I'm trying to do is, get access to private members of MyClass from non-firend function declared outside MyClass. Function shouldn't be static, and access to private members is needed via getter or not. I'm not aware of c++ possibilties. I appreciate any help!

ps: the "->" isn't enough, because the "_a" field is private. How would it looks with a getter?

edit2:

I unduerstand that some rules were broken but this code is an exercise I got from an university. It's specially done to show some approch. Unfortunatelly variable names are as they are, of course I could rename them but I forgot.

tylkonachwile
  • 2,025
  • 4
  • 16
  • 28
  • 2
    Maybe you meant to use ‘->’ ? –  Nov 18 '17 at 14:01
  • The message is self-explanatory. Instead of `kp2._a` in the function, use `kp2->_a`. That's how access of struct members via a pointer works. – Peter Nov 18 '17 at 14:03
  • 2
    Those members are private, that would be an obstacle if you do not provide getters. – Yunnosch Nov 18 '17 at 14:04
  • @Yunnosch - the function is a member, so has access to private members. – Peter Nov 18 '17 at 14:05
  • Op asks explicitly about non-friend function. So I assume that the `friend` used in the code quote is meant to go away with the answer to this question. – Yunnosch Nov 18 '17 at 14:05
  • Please elaborate what you mean by "non-friend global function". I would guess a function which is not a member of the class and not a friend either. But reading comments and answers, it seems not entirely clear to other readers. Do you know the meaning of a static member function/method? Would that be enough for your purpose? Are you aware of the meaning of the `private` in your class declaration? Is providing public "getters" an option? Those are members which allow to read (but e.g. not write) private member values. – Yunnosch Nov 18 '17 at 14:17
  • I know the meaning of the static keyword. What excatly I'm trying to do is, get access to private members of MyClass from non-firend function declared outside MyClass. Function shouldn't be static, and access to private members is needed via getter or not. I'm not aware of c++ possibilties. – tylkonachwile Nov 18 '17 at 14:18
  • 1
    Please edit your question to put that very helpful explanation directly in there. – Yunnosch Nov 18 '17 at 14:23
  • 1
    ok, i will edit post within 15 min max – tylkonachwile Nov 18 '17 at 14:26

1 Answers1

4

The diagnostic is quite clear and even gives a suggestion.

You need

  cout << "_a :" <<kp2->_a <<endl;
  //                  ^^

with a -> not a . in the body of WriteOutMyClass2

BTW, you might add assert(kp2 != nullptr); before that. If kp2 happens to be null or invalid, you have undefined behavior (and probably a segmentation fault) and that is scary.

You could declare your WriteOutMyClass2 as a static function (I'm not sure you want that, your intentions are unclear).

BTW, don't be afraid of friend functions, even if having too much of them is bad smell. You might want to (and in your case, you probably should) define many getters and setters.

I recommend spending several days reading more about C++ (at least C++11) since it is a very difficult programming language. So read a good C++ programming book and look into some C++ reference site.

Don't forget to enable all warnings and debug information, so compile on the command line with g++ -Wall -Wextra -g if using GCC. Learn how to use the debugger (e.g. gdb).

Be aware of the (empirical) rule of five (which your code breaks, and that is a mistake). Prefer standard containers and smart pointers. Perhaps you might remove size and declare std::vector<int> _c; instead of having c (and size) as private members.

Later on, consider defining your own operator << for output of your types.

Study for inspiration the source code of several free software projects (e.g. on github) coded in C++.

What exactly I'm trying to do is, get access to private members of MyClass

Wrong approach. You should define getter functions, and they generally are public: (even if some of them could be private: or protected:).

the "->" isn't enough, because the "_a" field is private. How would it looks with a getter?

Your current code is in very bad shape. But you might add a getter member function:

// inside class MyClass
public:
   int getA(void) const {return _a;};

and then use kp2->getA() instead of kp2->_a

BTW, you should consider more readable names (probably longer ones) for your public interface. If your member field _a is meant to be some price, its getter function should better be named getPrice (or perhaps just Price) than getA ...

It is important to keep your code readable (even to yourself, in a few months). Code is more often read than written (or compiled). Developer's time costs much more than computer time.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547