【发布时间】:2012-03-15 04:38:24
【问题描述】:
我认为我有一个循环依赖问题,不知道如何解决它.... 尽可能短: 我正在编写类似 html 解析器的代码。 我有一个 main.cpp 文件和两个头文件 Parser.h 和 Form.h。 这些头文件保存了整个定义...(我懒得制作相应的.cpp文件...
Form.h 看起来像这样:
//... standard includes like iostream....
#ifndef Form_h_included
#define Form_h_included
#include "Parser.h"
class Form {
public:
void parse (stringstream& ss) {
// FIXME: the following like throws compilation error: 'Parser' : is not a class or namespace name
properties = Parser::parseTagAttributes(ss);
string tag = Parser::getNextTag(ss);
while (tag != "/form") {
continue;
}
ss.ignore(); // >
}
// ....
};
#endif
Parser.h 看起来像这样:
// STL includes
#ifndef Parser_h_included
#define Parser_h_included
#include "Form.h"
using namespace std;
class Parser {
public:
void setHTML(string html) {
ss << html;
}
vector<Form> parse() {
vector<Form> forms;
string tag = Parser::getNextTag(this->ss);
while(tag != "") {
while (tag != "form") {
tag = Parser::getNextTag(this->ss);
}
Form f(this->ss);
forms.push_back(f);
}
}
// ...
};
#endif
不知道这是否重要,但我正在 MS Visual Studio Ultimate 2010 中进行构建 它把我扔了 'Parser' : 不是类或命名空间名称
如何解决这个问题? 谢谢!
【问题讨论】:
-
解决方案:别那么懒惰 ;)
-
@500-InternalServerError: :-) 所以这意味着我必须将定义和声明分开?会有帮助吗?
-
@Tomy:是的,没有分离几乎是不可能的。
-
@LightnessRacesinOrbit 好的,我可能说错了 - 我被教导分开做这件事并不重要,所以我决定不做......我不知道它是做到这一点至关重要......直到现在我只做非常简单的项目,所以我不需要制作单独的文件......
-
@Tomy:这不是“关键”,但它非常明智。这也是原因之一。谁教你“没那么重要”的人太傻了。
标签: c++ dependencies circular-dependency