I'm currently learning c++ and struggling with some 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_ */
The 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 just don't get it.
asked May 5, 2011 at 8:11
1 Answer 1
You have a cyclical include problem.
You're including Garden.h
in Utils.h
and Utils.h
in Garden.h
.
You either need to put both definitions in the same header file or forward declare one of the types.
However, you don't actually need to include Utils.h
in Garden.h
so removing that include should solve your problem.
answered May 5, 2011 at 8:16
Comments
lang-cpp
#include "Utils.h"
from Garden.h as you don't need it and this causes some problemsUtils.h
as I would like to only#include "Garden.h"
in my main program. Is there a way around?#include "Utils.h"
, create a new header, that includesUtils.h
andGarden.h
and then include the new header in your main program. Problem solved (: