Schilcote
Schilcote

Reputation: 2404

Expected primary-expression before various tokens

The following code:

class queuenode {
public:
    queuenode();
    queuenode(cargo *mydata);
    ~queuenode() {delete cargo;} //Queuenodes delete their cargo on destruction - note that queuenodes DON'T free their neighbour, that's up to the queue class
public:
    queuenode *next;
    int pri; //this should always be equal to our cargo's pri
    cargo *data;
};

queuenode::queuenode(void) {
    next= (queuenode *) NULL; 
    cargo = (cargo *) NULL; //default const creates a null queuenode - pri is left undefined
}

queuenode::queuenode(cargo *mydata) {
    //Convert constr creates a queuenode wrapping a cargo, also setting pri to its pri
    data=mydata; 
    pri=mydata->pri; 
    next=(queuenode *) NULL;
}

gives me the following errors:

pq_linkedlist.cpp: In destructor 'queuenode::~queuenode()':
pq_linkedlist.cpp:19:28: error: expected primary-expression before ';' token
pq_linkedlist.cpp: In constructor 'queuenode::queuenode()':
pq_linkedlist.cpp:28:8: error: expected unqualified-id before '=' token

I have no idea what's happening or why. There's all sorts of people with the same error message on the internet but all their problems seem to have to do with extra semicolons, and I'm pretty sure I don't have any.

Upvotes: 3

Views: 1742

Answers (1)

orlp
orlp

Reputation: 117846

~queuenode() {delete cargo;}

Should be:

~queuenode() {delete data;}

cargo is the type, data is the variable. Saying delete cargo makes as much sense as saying delete int.

Similarly this line is wrong:

cargo = (cargo *) NULL;

Upvotes: 6

Related Questions