Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 嵌套类C++;,如何使外部方法成为嵌套类的朋友_C++_Class_Objective C++ - Fatal编程技术网

C++ 嵌套类C++;,如何使外部方法成为嵌套类的朋友

C++ 嵌套类C++;,如何使外部方法成为嵌套类的朋友,c++,class,objective-c++,C++,Class,Objective C++,我需要从类stos的方法“pop”中的类信息中获取对私有数据的访问。我知道我可以使用一种特殊的方法修改嵌套函数,但我认为它不像使用“friend”那样是elegnat。我希望将外部方法作为嵌套类的朋友,但我得到的信息是“不能重载仅由返回类型区分的函数”。有可能吗 class stos { class info { int x; bool isGood; friend info pop(); // warning: cannot ov

我需要从类stos的方法“pop”中的类信息中获取对私有数据的访问。我知道我可以使用一种特殊的方法修改嵌套函数,但我认为它不像使用“friend”那样是elegnat。我希望将外部方法作为嵌套类的朋友,但我得到的信息是“不能重载仅由返回类型区分的函数”。有可能吗

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);   
};
编辑:


该代码可以很好地编译,但您可能希望执行以下操作:

#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;
}
#包括
使用名称空间std;
stos类
{
班级信息
{
int x;
布尔很好;
friend类stos;//允许stos访问信息的私有数据
info pop(){}
};
静态常数int SIZE=10;
国际丹麦[大小];
布尔是空的;
布尔满了;
int*top;
公众:
stos();
流行资讯(){
信息a;
a、 pop();//在这里调用私有方法
}
信息推送(intx);
};
int main(){
//你的密码在这里
返回0;
}

该代码可以很好地编译,但您可能需要执行以下操作:

#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;
}
#包括
使用名称空间std;
stos类
{
班级信息
{
int x;
布尔很好;
friend类stos;//允许stos访问信息的私有数据
info pop(){}
};
静态常数int SIZE=10;
国际丹麦[大小];
布尔是空的;
布尔满了;
int*top;
公众:
stos();
流行资讯(){
信息a;
a、 pop();//在这里调用私有方法
}
信息推送(intx);
};
int main(){
//你的密码在这里
返回0;
}

您可以在
stos
的开头声明
info
类,然后在以后定义它。因此,您可以将类的定义更改为

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
};

这将停止错误。

您可以在
stos
的开头声明
info
类,然后在以后定义它。因此,您可以将类的定义更改为

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
};

这将停止错误。

您使用哪种编译器?该代码在VisualStudio GCC(从4.3到4.9)和最新版本中编译良好。无论如何,在设计良好的应用程序中,您永远不需要“朋友”函数code@DarioOO它可以很好地编译,但不会使stos::pop成为朋友,而是一个未定义的全局函数。如果您尝试
朋友信息stos::pop()
,则会出现
错误:无效使用不完整的类型“class stos”
。我知道。用户发布的代码不会立即重现问题,如果我不知道他想如何通过显示有问题的代码片段来使用代码,那就帮不上忙了。@DarioOO所以你建议我使用一个函数返回私有数据的复制值?这是解决我问题的更好办法吗?@Puppy请给我一些建议。我还在学习,我没有注意到你为什么说我的代码设计得很糟糕。你用哪种编译器?该代码在VisualStudio GCC(从4.3到4.9)和最新版本中编译良好。无论如何,在设计良好的应用程序中,您永远不需要“朋友”函数code@DarioOO它可以很好地编译,但不会使stos::pop成为朋友,而是一个未定义的全局函数。如果您尝试
朋友信息stos::pop()
,则会出现
错误:无效使用不完整的类型“class stos”
。我知道。用户发布的代码不会立即重现问题,如果我不知道他想如何通过显示有问题的代码片段来使用代码,那就帮不上忙了。@DarioOO所以你建议我使用一个函数返回私有数据的复制值?这是解决我问题的更好办法吗?@Puppy请给我一些建议。我还在学习,我没有注意到你为什么说我的代码设计不好。但是当pop在作用域reach中时,为什么我应该在“friend info stos::pop()”中使用“stos::”(我的意思是声明友谊的代码在类stos中,所以我认为我不应该使用作用域运算符)@groosik引用了公认的答案:当您在类中声明具有非限定id的友元函数时,它会在最近的封闭命名空间范围中命名函数。这里的关键词是namaspace,因此
friend
不在作用域上使用运算符。您需要范围解析操作符!尝试删除
stos::
,代码将不再编译。简单来说,
friend stos::pop()
是类
stos
的方法。虽然
friend pop()
引用的是一个全局函数。但是当pop在作用域范围内时,为什么我要在“friend info stos::pop()”中使用“stos::”(我的意思是声明友谊的代码在类stos内,所以我认为我不应该使用作用域运算符)@groosik引用了公认的答案:当您在类中声明具有非限定id的友元函数时,它会在最近的封闭命名空间范围中命名函数。这里的关键词是namaspace,因此
friend
不在作用域上使用运算符。您需要范围解析操作符!尝试删除
stos::
,代码将不再编译。简单来说,
friend stos::pop()
是类
stos
的方法。而
friend pop()
正在引用全局函数。