简体   繁体   English

无法实例化好友函数中的类? 我没有在范围错误中声明

[英]Not able to Instantiate class inside friend function? I am getting not declared in scope error

I am not able to instantiate test class inside the friend function, the compiler throwing error ptr not declared in this scope. 我无法在Friendly函数中实例化测试类,编译器在此范围内未声明抛出错误ptr。 I believe friend functions have access to all the private and public members of the class yet I am getting this error. 我相信朋友功能可以访问该班级的所有私人和公共成员,但我遇到此错误。 I am not able to figure out where I am going wrong? 我无法弄清楚哪里出了问题?

#include<iostream>
using namespace std;

class test;
test* friendOfTest();

class test{
private: 
    static test* ptr;
public:
    friend test* friendOfTest();
    void someMethod(){ cout<<"someMethod()\n";}
};

test* test::ptr=NULL;

test* friendofTest(){
    ptr = new test; //Error,ptr not declared in this scope in this line
    return ptr;
}


int main(){
    test* t;
    t = friendofTest();
    t->someMethod();
    return 0;
}

Yes, you have access to ptr, but your syntax is wrong: 是的,您可以访问ptr,但是语法错误:

test* friendofTest(){
    test::ptr = new test; // note test::
    return test::ptr;
}

A friend function will not behave as a member function to your class, it just allows it's members to be accessed even though declared private or protected. 朋友函数将不会充当您类的成员函数 ,它仅允许访问其成员,即使已声明为私有或受保护。

friendofTest in this case will still be a completely separate function from your class, but you can access it's static test member through a scope resolution operator as usual, even though it's declared private. 在这种情况下, friendofTest仍然是类完全独立的函数,但是您可以照常通过范围解析运算符访问它的静态test成员,即使它被声明为私有的也是如此。

There are two possibilities to make the program to compile. 有两种方法可以使程序得以编译。

The first one as the friend function is defined outside the class is to use qualified names of the static class data members. 在类外部定义的第一个朋友函数是使用静态类数据成员的合格名称。 For example 例如

test* friendOfTest(){
    test::ptr = new test; //Error,ptr not declared in this scope in this line
    return test::ptr;
}

The second one is to define the function inside the class. 第二个是在类内部定义函数。 In this case it will be in the scope of the class. 在这种情况下,它将属于该类的范围。

According to the C++ Standard (11.3 Friends) 根据C ++标准(11.3版)

7 Such a function is implicitly inline. 7这样的函数是隐式内联的。 A friend function defined in a class is in the (lexical) scope of the class in which it is defined. 在类中定义的朋友功能在其定义的类的(词汇)范围内。 A friend function defined outside the class is not (3.4.1). 在类外定义的朋友函数不是(3.4.1)。

For example 例如

class test{
private: 
    static test* ptr;
public:
    friend test* friendOfTest();

friend test* friendOfTest(){
    ptr = new test; //Error,ptr not declared in this scope in this line
    return ptr;
}

    void someMethod(){ cout<<"someMethod()\n";}
};

Here are demonstrative programs 这是示范节目

#include<iostream>
using namespace std;

class test;
test* friendOfTest();

class test{
private: 
    static test* ptr;
public:
    friend test* friendOfTest();
/*
friend test* friendOfTest(){
    ptr = new test; //Error,ptr not declared in this scope in this line
    return ptr;
}
*/
    void someMethod(){ cout<<"someMethod()\n";}
};

test* test::ptr=NULL;

test* friendOfTest(){
    test::ptr = new test; //Error,ptr not declared in this scope in this line
    return test::ptr;
}


test* friendofTest();

int main(){
    test* t;
    t = friendOfTest();
    t->someMethod();
    return 0;
}

and

#include<iostream>
using namespace std;

class test;
test* friendOfTest();

class test{
private: 
    static test* ptr;
public:
//    friend test* friendOfTest();

friend test* friendOfTest(){
    ptr = new test; //Error,ptr not declared in this scope in this line
    return ptr;
}

    void someMethod(){ cout<<"someMethod()\n";}
};

test* test::ptr=NULL;
/*
test* friendOfTest(){
    test::ptr = new test; //Error,ptr not declared in this scope in this line
    return test::ptr;
}
*/

test* friendofTest();

int main(){
    test* t;
    t = friendOfTest();
    t->someMethod();
    return 0;
}

the both programs compile successfully. 这两个程序都能成功编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM