I'm getting an error "invalid use of incomplete type 'class map'

前端 未结 2 1271
我在风中等你
我在风中等你 2020-12-13 08:43

I am making a basic text based RPG sorry if my question is stupid because im new to c++. So basically I have a small combat class that i have to link back and forth to from

2条回答
  •  悲&欢浪女
    2020-12-13 09:06

    Your first usage of Map is inside a function in the combat class. That happens before Map is defined, hence the error.

    A forward declaration only says that a particular class will be defined later, so it's ok to reference it or have pointers to objects, etc. However a forward declaration does not say what members a class has, so as far as the compiler is concerned you can't use any of them until Map is fully declared.

    The solution is to follow the C++ pattern of the class declaration in a .h file and the function bodies in a .cpp. That way all the declarations appear before the first definitions, and the compiler knows what it's working with.

提交回复
热议问题