我有一个程序可以扫描文件并将所有内容保存在标记向量中(我创建的用于存储每个标记的类)。之后,它解析所有令牌,并查看是否采用正确的格式(正确的语法)。我使用迭代器抛出每个标记。但是,出于某种原因,我的类Parser不喜欢我声明迭代器的方式。这是我的主要功能:
int main(){
DatalogProgram DLP;
bool passed = true;
ifstream in;
Scanner scanner;
in.open("File7.txt");
// in.open(argv[1]);
Parser parse(scanner.getTokens(), &DLP);
scanner.scan(in);
try {
parse.checkDatalog();
}
catch (Token* token) {
cout << "Failure!\n ";
cout << "(" << token->getType(token->type) << ",\"" << token->value << "\"," << token->lineNumber << ")";
passed = false;
}
if (passed)
{
cout << "Success!\n";
DLP.printScheme();
DLP.printFact();
DLP.printRule();
DLP.printQuery();
}
system("pause");
return 0;
}
这是我的Parser头文件,我声明了我的迭代器:
#pragma once
#include "DatalogProgram.h"
#include "Predicate.h"
#include "Rule.h"
#include "Token.h"
#include <set>
#include <vector>
using namespace std;
class Parser : public Token {
public:
Parser() { it = tokens.begin(); }
Parser(vector<Token*> tokens, DatalogProgram *DLP) { this->tokens = tokens; this->DLP = DLP; it = tokens.begin(); }
~Parser() { }
bool checkDatalog();
void checkScheme();
void checkSchemeList();
void checkIdList(Predicate* scheme);
void checkFact();
void checkFactList();
void checkQuery();
void checkQueryList();
void checkStringList(Predicate* pred);
void checkRule();
void checkRuleList();
void checkHeadPredicate(Rule* rule);
Predicate* checkPredicate();
void checkPredicateList(Rule* rule);
string checkParameter();
void checkParameterList(Predicate* pred);
string checkExpression();
string checkOperator();
bool match(TokenType second, Token *token);
void domainPrinter();
private:
vector<Token*> tokens;
DatalogProgram *DLP;
vector<Token*>::iterator it;
set<string> domain;
stringstream expression;
};
DatalogProgram.h:
`#pragma once
#include "Scanner.h"
#include "Rule.h"
#include "Predicate.h"
#include "Parser.h"
//using namespace std;
class DatalogProgram : public Token {
public:
DatalogProgram() { }
~DatalogProgram() { }
inline void addScheme(Predicate scheme) { schemes.push_back(scheme); }
inline void addRule(Rule rule) { rules.push_back(rule); }
inline void addFact(Predicate fact) { facts.push_back(fact); }
inline void addQuery(Predicate query) { queries.push_back(query); }
void printScheme();
void printFact();
void printRule();
void printQuery();
void printPredicate(vector <Predicate> printing);
vector<Predicate> schemes;
vector<Rule> rules;
vector<Predicate> facts;
vector<Predicate> queries;
};
`
答案 0 :(得分:1)
显示的头文件完全坏了。
main
翻译单元最有可能包含DatalogProgram.h
标题。
DatalogProgram.h
会立即包含Parser.h
:
#pragma once
/* ... */
#include "Parser.h"
然后Parser.h
再次包含Datalog.h
:
#pragma once
#include "DatalogProgram.h"
但是,由于已经包含DatalogProgram.h
,#pragma once
完全忽略了这一点。编译然后继续,直到:
Parser(vector<Token*> tokens, DatalogProgram *DLP)
但由于尚未定义DatalogProgram
,因此尚未定义此类。这会导致编译错误。
您需要确定您的头间文件依赖项应该是什么,并修复循环依赖项;最有可能的是使用前向声明。
答案 1 :(得分:0)
有一个递归的头依赖项。这不会在C ++中起作用。在Parser.h中转发声明DatalogProgram
并删除Parser.h对DatalogProgram.h的依赖
#include "DatalogProgram.h" <-- Delete this line.
struct DatalogProgram;
class Parser : public Token {
....