Typedef in the same namespace in different header files

I am currently learning C ++ and struggling with some kind of code:

Garden.h:

#ifndef GARDEN_H_ #define GARDEN_H_ #include <vector> #include <boost/tr1/memory.hpp> #include "Shape.h" #include "Utils.h" namespace cma { typedef boost::shared_ptr<cma::Shape> ShapePtr; typedef std::vector<cma::ShapePtr> GardenPlan; } #endif /* GARDEN_H_ */ 

Utils.h:

 #ifndef UTILS_H_ #define UTILS_H_ #include "Garden.h" namespace cma { void printList(GardenPlan const & p, std::ostream & o); } #endif /* UTILS_H_ */ 

Compiler Output:

 In file included from ../src/Garden.h, from ../src/Utils.cpp: ../src/Utils.h: error: variable or field 'printList' declared void ../src/Utils.h: error: 'GardenPlan' was not declared in this scope ../src/Utils.h: error: expected primary-expression before '&' token ../src/Utils.h: error: 'o' was not declared in this scope 

I seriously do not understand.

+4
source share
1 answer

You have a cyclical problem.

You include Garden.h in Utils.h and Utils.h in Garden.h .

You need to either put both definitions in the same header file, or forward the declaration of one of the types.

However, in fact, you do not need to include Utils.h in Garden.h , so removing this problem should solve your problem.

+4
source

All Articles