illegal member initialization

Error C2614 'Ares::ObjectManagement::Entity': illegal member initialization: 'm_manager' is not a base or member


1
2
3
4
5
6
7
8
9
10
		class Entity
		{
		private:
			Manager *m_manager;
			bool m_alive{true};

		public:
			bool isAlive()const { return m_alive; }
			Entity(Manager &manager) : m_manager(&manager) {}
		};
1
2
3
4
class Manager; //forward declaration
class Entity{
   //...
};
Can't I have a #include headers.h included?
Yes, you can, as long as they don't include each other.
Can't I use pragma once to solve the issue of having them include each other?
Where it is supported, #pragma once usually prevents the same file from being included twice. This avoids multiple definition errors, but it does not eliminate the case where a definition in file A depends on the contents of file B, and vise-versa.
A.hpp
1
2
3
# pragma once
# include "B.hpp"
struct Manager: Entity {};

B.hpp
1
2
3
# pragma once
# include "A.hpp"
struct Entity { Manager* m; };


This case can be resolved using a forward declaration:
A.hpp
1
2
3
# pragma once
# include "B.hpp"
struct Manager: Entity {}; 


B.hpp
1
2
3
# pragma once
struct Manager; 
struct Entity { Manager* p };


Usually # pragma once is equivalent to the include-guard trick, which is more portable:
A.hpp
1
2
3
4
# if ! defined YOUR_PROJECT_A_HPP_INCLUDED
# define YOUR_PROJECT_A_HPP_INCLUDED
// header contents here
# endif 
Last edited on
Topic archived. No new replies allowed.