0

我的代码中有一个名为“schemeList”的对象,如下所示:

class scheme;
class schemeList
{
    friend class scheme;
private:

    scheme * head;
    schemeList * tail;

public:
    schemeList();
    Token addScheme(vector <Token> &toAdd);
    //...other functions
};

我遇到问题的函数是“addScheme”,它应该向头部添加一个方案,或者如果头部已满,则将其添加到尾部(以链表的方式)。这是我到目前为止的功能:

Token schemeList::addScheme(vector<Token>& toAdd)
{
    if (head->retCloseParen() != ')')
    {
        scheme * headCopy = head;       
        Token answer = head->addScheme(toAdd);
        head = headCopy;
        return  answer;
    }
    else
    {
        //deal with tail
        schemeList * arrow = this;

        while (arrow->tail != NULL)
        {
            arrow = arrow->tail;
        }

        tail = new schemeList();

        tail->head= new scheme();
        tail->tail = NULL;

        Token answer = arrow->tail->addScheme(toAdd);
        return answer;
    }
}

这适用于添加第一个方案,但第二个方案会抛出一个错误,显示“未处理的异常......”。我之前遇到过这样的问题,但我toAdd通过引用传递变量而不是传递整个对象来修复它。为什么这会引发错误和/或我该如何解决?

如果它使事情更清楚,一个方案是:

class scheme
{
    friend class identifierList;

public:
    Token * id;
    char openParen;
    char closeParen;
    identifierList * idList;

    scheme();
    //other functions
};

class identifierList
{
    friend class scheme;
public:
    Token * id;
    identifierList * next;

    identifierList(Token * inId, identifierList * inNext);
};

一个令牌是:

class Token
{
    friend class datalogProgram;

public:
    int lineNumber;
    string type;
    string value;

    Token(string inType, string inValue, int inLineNum);
    //other functions
};
4

1 回答 1

0
  • 没有给出足够的细节,尤其是导致异常的代码。你最好调查调用堆栈!
  • 您是否正在初始化head, 和其他指针变量NULL- 我没有看到任何构造函数这样做。也没有任何检查(它只是head->retCloseParen() != ')'检查)。
  • 正如 Thomas 在评论中所建议的,您应该更好地使用一些已经为您提供的容器类。
于 2013-03-31T18:09:44.480 回答