Prototype structure?

How to put the structure in a separate file? I can do this with functions by placing a function prototype in a header file, for example. file.h and the function body in the file, for example file.cpp, and then using the include directive #include "file.h" in the source file with main. Can someone give a simple example to do the same with a structure like the one below? I am using dev-c ++.

struct person{
  string name;
  double age;
  bool sex;
};
+3
source share
2 answers

Just announce

struct person;

It is called class forward ad . In C ++, structures are classes with all public members by default.

+6
source

:

person.h:

#ifndef PERSON_H_
#define PERSON_H_
struct person{ 
  string name; 
  double age; 
  bool sex; 
};
#endif

person.h .cpp , .

() :

person.h:

#ifndef PERSON_H_
#define PERSON_H_
struct person{ 
  string name; 
  double age; 
  bool sex; 
};
extern struct Person some_person;
#endif

.cpp , 'some_person'

struct Person some_person;

.cpp, some_person, person.h.

+3

Source: https://habr.com/ru/post/1713601/


All Articles