0

I am trying to use a struct of one class in another class. My first class looks like this:

Class1.h

#ifndef CLASS1_H
#define CLASS1_H

class Class1
{
public:
    Class1();
    ~Class1();

    struct St{
        int x;
    }

    St struct1;

private:

};

#endif

Now in the header of the second class, I want to use this struct and variable.

Class2.h

#ifndef CLASS2_H
#define CLASS2_H

#include "Class1.h"

class Class2
{
public:

    Class2();
    ~Class2();

    St struct2;

private:

};

However, it says:

identifier "St" is undefined.

When I use it this way:

Class1::St struct1;

the error goes away. What are the issues with my code?

Community
  • 1
  • 1
Hadi GhahremanNezhad
  • 2,377
  • 5
  • 29
  • 58

1 Answers1

1

What are the issues with my code?

The issue is St is a nested class. Its fully qualified name is ::Class1::St1. Within the scope of Class2, the unqualified name lookup does not use the scope of Class1, so no declaration of St will be found.

You can fix the issue by doing the following:

When I use it this way:

Class1::St struct1;

the error goes away.

eerorika
  • 232,697
  • 12
  • 197
  • 326