0

I am not able to instantiate test class inside the friend function, the compiler throwing error ptr not declared in this scope. 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;
}
Sadik Khan
  • 29
  • 3

2 Answers2

1

Yes, you have access to ptr, but your syntax is wrong:

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.

Steeve
  • 1,323
  • 1
  • 12
  • 23
  • I tried to access ptr using scope resolution like you said but I am still getting this strange error. In function ‘test* friendofTest()’ error: ‘test* test::ptr’ is private test* test::ptr=NULL; ^ error: within this context test::ptr = new test; – Sadik Khan Feb 24 '17 at 14:52
  • @SadikKhan Check [this](http://stackoverflow.com/questions/29574616/friend-functions-and-static-data-members) question. – Steeve Feb 24 '17 at 15:01
0

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)

7 Such a function is implicitly inline. 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).

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335