I have 2 header files and a source file,
food.h:
#ifndef _FOOD_H
#define _FOOD_H
struct food {
char name[10];
};
#endif
dog.h:
#ifndef _DOG_H
#define _DOG_H
class food; //forward declaration
class Dog {
public:
Dog(food f);
private:
food _f;
};
#endif
And here is the source file of class dog
,
//dog.cpp
#include "dog.h"
#include "food.h"
Dog::Dog(food f) : _f(f)
{}
The problem is I can compile the above code, I got an error saying _f has incomplete type
.
I thought I can put a forward declaration in dog.h
and include food.h
in dog.cpp
, but it doesn't work, why? And I shouldn't put user defined header file in .h
files, right? It's deprecated, isn't it?