Martin Melka
Martin Melka

Reputation: 7799

Expected unqualified-id before token error

I'm starting with C++ and I can't figure this out. I have three classes and am trying to implement a queue. (Doesn't matter if it works or not now, i just need to fix this error)

#include <cstdlib>
#include <iostream>
#include "queue.h"

using namespace std;

int main(int argc, char** argv) {

    queue fronta();

    queue.add(10); // <- expected unqualified-id before ‘.’ token
}

queue.h:

#ifndef QUEUE_H
#define QUEUE_H

#include "queueItem.h"

class queue {
private:
    queueItem* first;
    queueItem* last;

public:
    queue();
    void add(int number);
    int get(void);
    bool isEmpty();
};

#endif  /* QUEUE_H */

queueItem.h:

#ifndef QUEUEITEM_H
#define QUEUEITEM_H

class queueItem{
private:
    int value;
    queueItem* next;

public:
    queueItem(int value);

    int getValue();
    queueItem* getNext();
    void setNext(queueItem* next);
};

#endif  /* QUEUEITEM_H */

From what I've googled, it's usually related to extraneous semicolon, bracket or such. I found nothing of sorts though

Thanks for help

Upvotes: 1

Views: 2648

Answers (2)

us2012
us2012

Reputation: 16263

You can't call .add() on the class type queue, you need to call it on the object you have created! In your case, that would be fronta.add(10);.

Also, your syntax for creating fronta is wrong. Use queue fronta;.

Upvotes: 2

Andy Prowl
Andy Prowl

Reputation: 126582

The line queue fronta(); is declaring a function which returns an object of type queue and takes no argument. This is likely not what you want. Use queue fronta; instead.

Secondly, you have to call the function add() on an instance of queue, not on the class itself (that would be the case if the function were static, but in that case you would use :: instead of .). Therefore:

queue.add(10); // ERROR!
fronta.add(10); // OK

Upvotes: 2

Related Questions