4

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
6
  • 3
    Note the typo in your Garden.h header guard. Commented May 5, 2011 at 8:16
  • 1
    You can remove the #include "Utils.h" from Garden.h as you don't need it and this causes some problems Commented May 5, 2011 at 8:19
  • The typo wasn't the source of the failure. And I don't want to remove Utils.h as I would like to only #include "Garden.h" in my main program. Is there a way around? Commented May 5, 2011 at 8:25
  • possible duplicate of Classes as parameters error Commented May 5, 2011 at 8:25
  • 1
    @cimnine - yes, there is: remove #include "Utils.h", create a new header, that includes Utils.h and Garden.h and then include the new header in your main program. Problem solved (: Commented May 5, 2011 at 8:27

1 Answer 1

4

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.