0

I have an object in my code called a "schemeList" that looks like this:

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

    scheme * head;
    schemeList * tail;

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

The function I'm having a problem with is "addScheme" which is supposed to add a scheme to the head, or if the head's full, add it to the tail (in a linked-list kinda way). Here's the function I have so far:

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;
    }
}

This works fine for adding the first scheme, but the second scheme throws an error that says "Unhandled Exception...". I had a problem like this before but I fixed it by passing the variable toAdd by reference instead of passing the whole object. Why is this throwing the error and/or how do I fix it?

In case it makes things more clear, a scheme is:

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);
};

And a token is:

class Token
{
    friend class datalogProgram;

public:
    int lineNumber;
    string type;
    string value;

    Token(string inType, string inValue, int inLineNum);
    //other functions
};
Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
user1311736
  • 83
  • 2
  • 2
  • 7

1 Answers1

0
  • Not enough details given, especially the code which is causing the exception. You better investigate the call-stack!
  • Are you initializing head, and other pointer variables with NULL - I don't see any constructor that does it. Nor there are any checks (its just head->retCloseParen() != ')' check).
  • As suggested by Thomas in comment, you should better use some of the container classes already available for you.
Ajay
  • 18,086
  • 12
  • 59
  • 105