未定义的函数引用

时间:2013-05-19 13:58:49

标签: c++ compilation include header-files

由于一些链接错误,我似乎无法编译我的项目,但我找不到它。 我有一个解析器类,其中使用了扫描器类的方法。并且编译器在parser.h或parser.cpp中扫描器的公共部分(get_token(),get_line(),scanner(const char ),~scanger())中的任何函数的每个条目都标记了链接错误(“undefined”参考scanner :: “)。请帮助我解决这个问题,因为我不知道是什么原因造成的。这是代码。 parser.h:

#ifndef PARSER_H_INCLUDED
#define PARSER_H_INCLUDED

#include "scanner.h"
#include <iostream>

class parser
{
    scanner s;
    token cur_token;

    void next_token();

    void parse_P_EXPR();
    void parse_TERM();
    void parse_STATEMENTS();
    void parse_STATEMENT_ID();
    void parse_CHILDREN();
    void parse_CHILDREN_TERM();

public:

    parser(const char *);
};

#endif // PARSER_H_INCLUDED

scanner.h:

#ifndef SCANNER_H_INCLUDED
#define SCANNER_H_INCLUDED

#include <fstream>
#include <string>
#include "token.h"

class scanner
{
    enum type_of_state
    {
        H,
        ID,
        STRING,
        OPERATION
    };
    type_of_state state;
    static const char *reserved_words[];
    static const char *operations[];
    std::string buf;
    size_t line;
    int c;
    FILE *f;

    void add();
    void clear();
    void get();
    int search(std::string, const char **);
    void unget();

public:

    token get_token();
    size_t get_line();
    scanner(const char *);
    ~scanner();
};

#endif // SCANNER_H_INCLUDED

scan.cpp:

#include "scanner.h"

const char *scanner::reserved_words[] =
{
    "loop",
    "else",
    "when",
    "print",
    "println",
    "die",
    0
};

const char *scanner::operations[] =
{
    "::",
    "<-",
    "||",
    0
};

void scanner::add() {
    buf += c;
}

void scanner::clear() {
    buf = "";
}

void scanner::get() {
    c = fgetc(f);
}

int scanner::search(std::string buf, const char **list) {
    int i = 0;
    while (list[i]) {
        if (buf == list[i])
            return i + 1;
        ++i;
    }
    return 0;
}

void scanner::unget() {
    ungetc(c, f);
}

size_t scanner::get_line() {
    return line;
}

scanner::scanner(const char *text) {
    f = fopen(text, "r");
    if (f == 0)
        throw scanner_exception(WRONG_FILENAME);
    line = 1;
    state = H;
    clear();
    get();
}

scanner::~scanner() {
    fclose(f);
}

token scanner::get_token() {
    state = H;
    int i;
    do {
        switch(state) {
            case H:
                if (c == '\n') {
                    get();
                    ++line;
                } else if (c == ' ' || c == '\r' || c == '\t') {
                    get();
                } else if (isalpha(c)) {
                    clear();
                    add();
                    get();
                    state = ID;
                } else if (c == '"') {
                    clear();
                    get();
                    state = STRING;
                } else if (c == ':' || c == '<' || c == '|') {
                    clear();
                    add();
                    get();
                    state = OPERATION;
                } else if (c == '(') {
                    get();
                    return token(token::LBRACKET);
                } else if (c == ')') {
                    get();
                    return token(token::RBRACKET);
                } else if (c == ';') {
                    get();
                    return token(token::SEMICOLON);
                } else if (c == '@') {
                    return token(token::END);
                } else
                    throw scanner_exception(c, line, "Unexpected symbol");
                break;
            case ID:
                if (isalpha(c) || isdigit(c) || c == '_') {
                    add();
                    get();
                } else if (i = search(buf, reserved_words)) {
                    return token((token::type_of_token) i, buf);
                } else
                    return token(token::ID, buf);
                break;
            case STRING:
                if (c == '\n' || c == '@') {
                    throw scanner_exception(c, line, "Unexpected end of string");
                }
                if (c != '"') {
                    add();
                    get();
                } else {
                    get();
                    return token(token::STRING, buf);
                }
                break;
            case OPERATION:
                add();
                if (i = search(buf, operations)) {
                    get();
                    return token((token::type_of_token) (i + token::COLON));
                } else if (buf[0] == ':') {
                    unget();
                    return token(token::COLON);
                } else
                    throw scanner_exception(c, line, "Unexpected symbol");
                break;
        }
    }
    while (true);
}

0 个答案:

没有答案