在不同的头文件中跨同一命名空间的typedef

时间:2011-05-05 08:11:06

标签: c++ namespaces typedef

我目前正在学习c ++并且正在努力学习一些代码:

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_ */

编译器输出:

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

我真的不明白。

1 个答案:

答案 0 :(得分:4)

你有一个周期性的包含问题。

您在Garden.hUtils.h Utils.h中加入了Garden.h

您需要将两个定义放在同一个头文件中,或者转发声明其中一个类型。

但是,您实际上不需要在Utils.h中加入Garden.h,因此删除该内容可以解决您的问题。