accessing a private structure in a class using member function

Trying to write a setter function for a private structure inside a class. Haven't found a declaration method that allows access to the structure.

The structure may not be moved outside of the class. The function must be a member of the class. Forward declarations may not be used.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    class Editor
    {
    public:
        void setName ( string s );
    private:
        struct Object
        {
            string name;
        }Instance;
    }Ed;
    
    void Editor::setName ( string s )
    {
        name = s;  // no access
    }

==================================
    
    class Editor
    {
    public:
        friend void setName ( Editor &m , string s );
    private:
        struct Object
        {
            int name;
        }Instance;
    }Ed;
    
    void setName ( Editor &m , string s )
    {
        name = s; // no access
    }
    
==================================

    class Editor
    {
    public:
        friend void setName ( Object &m , string s ); //invalid declaration (  Object is undefined )
    private:
        struct Object
        {
            string name;
        }Instance;
    }Ed;
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

using namespace std;

class Editor
{
    public:
            void setName ( string s );
    private:
            struct Object
            {
                string name;
            }   Instance;
};

    void Editor::setName ( string s )
    {
        Instance.name = s;
    }

using namespace std;

int main()
{

}
Lines 10,28,46: Why are you creating a global instance of you class called Ed?
Globals should be avoided. Note that GunnerFunner's snippet does not create this instance. If you want to create an instance of your Editor class, you should do so inside main().
Topic archived. No new replies allowed.