循环依赖...如何解决? [英] cyclic dependency ... how to solve?

查看:750
本文介绍了循环依赖...如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我有一个循环依赖问题,不知道如何解决它....
要尽可能短:
我正在编码像一个html解析器。
我有一个main.cpp文件和两个头文件Parser.h和Form.h.
这些头文件占有整个定义...(我太懒了,要做出相应的.cpp文件...



Form.h看起来像这个:

  // ...标准包括像iostream .... 

#ifndef Form_h_included
#define Form_h_included

#includeParser.h
class Form {
public:
void parse(stringstream& ss){

// FIXME:以下类似于throws编译错误:'Parser':不是类或命名空间名称
properties = Parser :: parseTagAttributes(ss);

string tag = Parser :: getNextTag(ss);
while(tag!=/ form){
continue;
}
ss.ignore(); //>
}
// ....
};
#endif

和Parser.h如下所示:

  // STL包含
#ifndef Parser_h_included
#define Parser_h_included

#includeForm.h

using命名空间std;

class Parser {
public:
void setHTML(string html){
ss< HTML;
}
向量< Form> parse(){
vector< Form>形式;

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':不是类或命名空间名称



如何解决这个问题?
谢谢!

解决方案

你可能想在这里做的是将标题中的方法声明留下, / p>

  class Form {
public:
void parse(stringstream& ss);
// ....
};

在源文件(即Form.cpp文件)中定义方法如下

  #includeForm.h
#includeParser.h

void parse stringstream& ss){

properties = Parser :: parseTagAttributes(ss);

string tag = Parser :: getNextTag(ss);
while(tag!=/ form){
continue;
}
ss.ignore(); //>
}

这应该可以解决你看到的循环依赖问题...


I think I have a cyclic dependency issue and have no idea how to solve it.... To be as short as possible: I am coding something like a html parser. I have a main.cpp file and two header files Parser.h and Form.h. These header files hold the whole definitions... (I'm too lazy to make the corresponding .cpp files...

Form.h looks like this:

//... 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

and Parser.h looks like this:

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

Don't know if it is important, but I'm doing the build in MS Visual Studio Ultimate 2010 and it throws me 'Parser' : is not a class or namespace name

How to solve this problem? Thank you!

解决方案

What you probably want to do here is leave the method declaration in the header like so

class Form {
public:
    void parse (stringstream& ss);
// ....
};

And define the method in a source file (i.e. a Form.cpp file) like so

#include "Form.h"
#include "Parser.h"

void parse (stringstream& ss) {

    properties = Parser::parseTagAttributes(ss);

    string tag = Parser::getNextTag(ss);
    while (tag != "/form") {
        continue;
    }
    ss.ignore(); // >
}

That should resolve the cyclic dependency issue you're seeing...

这篇关于循环依赖...如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆