0

I need to get acces to private data from class info in method "pop" from class stos. I know that I can use a special method modificating nested function, but I think that it isn't elegnat as using "friend". I'd like to make the external method as a friend for nested class, but I'm getting information "cannot overload functions distungished by return type alone". Is it possible to make that?

class stos
{
    class info
    {
        int x;
        bool isGood;
        friend info pop(); // warning: cannot overload functions distungished by return type alone
    };
    static const int SIZE = 10;
    int dane[SIZE];
    bool isEmpty;
    bool isFull;
    int *top;
public:
    stos();
    info pop();
    info push(int x);   
};

EDIT:

   stos::info stos::pop()
{
    info objInfo;
    if (isEmpty == true)
    {
        objInfo.isGood = false;
        return objInfo;
    }

    objInfo.isGood = true;
    objInfo.x = *top;
    top--;
    return objInfo;

}
jakub gros
  • 11
  • 1
  • 1
  • 3
  • wich compiler do you use? that code compile fine in VisualStudio GCC (from 4.3 to 4.9) and latest Clang ^^. And anyway you never need "friend" functions in good designed code – CoffeDeveloper May 22 '15 at 16:26
  • @DarioOO it compiles fine but doesn't make the `stos::pop` friend, but a global function `pop` which is not defined. If you try `friend info stos::pop()`, then you get `error: invalid use of incomplete type 'class stos'`. – vsoftco May 22 '15 at 16:28
  • I know. The user posted a code wich does not immediatly reproduce the problem, can't be of more help if I don't know how he want to use the code by showing a problematic snippet. – CoffeDeveloper May 22 '15 at 16:31
  • @DarioOO so you suggest me to use a function returning copied value of private data? Is it better solution for my problem? – jakub gros May 22 '15 at 16:37
  • @Puppy Please give me some tips. I'm still learning and I didn't notice why you said that my code is bad designed. – jakub gros May 22 '15 at 16:37

2 Answers2

3

You can declare the info class at the start of stos and then define it later. So you can change the definition of your class to this

class stos
{
    class info;
    ^^^^^^^^^^ Declare it here

... rest of class

public:
    info pop();

private:
    class info
    {
        int x;
        bool isGood;
        friend info stos::pop(); 
                    ^^^^ Added surrounding class for overload resolution
    }; //Define it here
};

That should stop the error.

phantom
  • 3,292
  • 13
  • 21
  • but why sould I use "stos::" in " friend info stos::pop()" when pop is in the scope reach (I mean that the code of declarating friendship is inside of class stos, so I think that I shouldn't use scope operator). – jakub gros May 22 '15 at 16:42
  • @groosik http://stackoverflow.com/q/8207633/3093378 Citing from the accepted answer: *When you declare a friend function with an unqualified id in a class it names a function in the nearest enclosing namespace scope.* The key word here is *namaspace*, so `friend` does not operator on *scope*. You need the scope resolution operator! Try removing `stos::` [here](http://ideone.com/m672nB) and the code won't compile anymore. – vsoftco May 22 '15 at 16:47
  • in simple terms `friend stos::pop()` is the method of class `stos`. while `friend pop()` is refererring to a global function. – CoffeDeveloper May 23 '15 at 09:34
1

that code compile fine however you may wanted to do the following:

#include <iostream>
using namespace std;

class stos
{
class info
{
        int x;
        bool isGood;
        friend class stos; //allow stos accessing private data of info
        info pop(){}
    };
    static const int SIZE = 10;
    int dane[SIZE];
    bool isEmpty;
    bool isFull;
    int *top;
public:
    stos();
    info pop(){
        info a;
        a.pop(); //calling here a private method
    }
    info push(int x);


};

int main() {
    // your code goes here
    return 0;
}
CoffeDeveloper
  • 7,961
  • 3
  • 35
  • 69