编译失败并显示“expected unqualified-id”关于“使用”

发布于 2024-10-30 17:39:46 字数 2904 浏览 0 评论 0原文

我需要一些帮助来制作这个课堂程序。我们正在使用 g++(linux?它通过服务器上的 putty)我对这个链接列表有很多问题。它给我的当前错误是

queue.cpp:2: 错误:“using”之前预期有不合格的 id

想知道是否有人可以帮助我解决这个问题。稍微搜索了一下发现问题似乎出在#define的某个地方?该错误显示它位于 .cpp 文件中,但我认为它位于 .h 文件中。另外,如果你能给我任何关于任何看起来不对劲、错误的编程建议,或者是否有更好的方法。

这里的queue.cpp 文件下面的queue.h 文件

#ifndef QUEUE_H
#define QUEUE_H

template <class Object>
class Queue
{
 public:
     Queue();
     Queue(const Queue& a_queue);
     Queue& operator =(const Queue& rhs);
     bool enqueue(const Object& d);
     bool dequeue(const Object& d);
     bool isEmpty() const;
     ~Queue();

private:
    struct ListNode
    {
        Object obj;
        ListNode *next;

    };
    ListNode *head;
}

#endif //Queue_H
#include "queue.cpp"  //include queue.cpp with file

#include <iostream>
using namespace std;
template <class Object>
Queue<Object>::Queue()
{
    head = NULL;
}

template <class Object>
Queue<Object>::Queue(const Queue<Object>& a_queue)
{
    head=NULL;
    *this=a_queue;
}

template <class Object>
Queue<Object>& Queue<Object>::operator =(const Queue<Object> &rhs)
{
    ListNode *nodePtr;
    nodePtr = rhs.head;
    if(this != rhs){
        this.head = NULL;
        while(nodePtr != NULL){
             this.enqueue(nodePtr->obj);
             nodePtr = nodePtr->next;
        }
    }
}

template <class Object>
bool Queue<Object>::enqueue (const Object& d) //Enqueue
{
    ListNode *newNode;
    ListNode *nodePtr;
    ListNode *previousNode;
    previousNode = NULL;
    nodePtr = NULL;
    newNode = new ListNode;
    newNode->obj = d;
    newNode->next = NULL;

    if(isEmpty){
        head = newNode;
        return true;
        }
    else{
        nodePtr = head;
        previousNode = NULL;
        while(nodePtr != NULL){
            previousNode = nodePtr;
            nodePtr=nodePtr->next;
        }
        if(previousNode->next == NULL){
            previousNode->next = newNode;
            return true;
        }
        else
            return false;
    }
}

template <class Object>
bool Queue<Object>::dequeue (const Object& d)  //Dequeue
{
    ListNode *nodePtr;

    if(!head)
        return false;
    else{
        if(head->next != NULL){
            nodePtr = head;
            d=nodePtr->obj;
            head = head->next;
            delete nodePtr;
        }else
            delete head;
        return true;
    }
}

template <class Object>
bool Queue<Object>::isEmpty() const  //Check if Empty
{
    if(!head)
        return true;
    else
        return false;
}

template <class Object>
Queue<Object>::~Queue()   //Destructor
{
    Object temp;
    while (head != NULL)
        dequeue (temp);
}

I need some help making this program for class. We are working with g++ (linux? its through putty on a server) I am having a lot of issue with this linked list. The current error that it is giving me is

queue.cpp:2: error: expected unqualified-id before âusingâ

Wondering if anyone could help me figure it out. A little bit of searching shows that the problem seems to be in the #define somewhere? The error shows that it is in the .cpp file but i think it is in the .h file. Also if you could give me any programing advise about anything that seems off, wrong or if there a better way of doing it.

the queue.h file below

#ifndef QUEUE_H
#define QUEUE_H

template <class Object>
class Queue
{
 public:
     Queue();
     Queue(const Queue& a_queue);
     Queue& operator =(const Queue& rhs);
     bool enqueue(const Object& d);
     bool dequeue(const Object& d);
     bool isEmpty() const;
     ~Queue();

private:
    struct ListNode
    {
        Object obj;
        ListNode *next;

    };
    ListNode *head;
}

#endif //Queue_H
#include "queue.cpp"  //include queue.cpp with file

the queue.cpp file here.

#include <iostream>
using namespace std;
template <class Object>
Queue<Object>::Queue()
{
    head = NULL;
}

template <class Object>
Queue<Object>::Queue(const Queue<Object>& a_queue)
{
    head=NULL;
    *this=a_queue;
}

template <class Object>
Queue<Object>& Queue<Object>::operator =(const Queue<Object> &rhs)
{
    ListNode *nodePtr;
    nodePtr = rhs.head;
    if(this != rhs){
        this.head = NULL;
        while(nodePtr != NULL){
             this.enqueue(nodePtr->obj);
             nodePtr = nodePtr->next;
        }
    }
}

template <class Object>
bool Queue<Object>::enqueue (const Object& d) //Enqueue
{
    ListNode *newNode;
    ListNode *nodePtr;
    ListNode *previousNode;
    previousNode = NULL;
    nodePtr = NULL;
    newNode = new ListNode;
    newNode->obj = d;
    newNode->next = NULL;

    if(isEmpty){
        head = newNode;
        return true;
        }
    else{
        nodePtr = head;
        previousNode = NULL;
        while(nodePtr != NULL){
            previousNode = nodePtr;
            nodePtr=nodePtr->next;
        }
        if(previousNode->next == NULL){
            previousNode->next = newNode;
            return true;
        }
        else
            return false;
    }
}

template <class Object>
bool Queue<Object>::dequeue (const Object& d)  //Dequeue
{
    ListNode *nodePtr;

    if(!head)
        return false;
    else{
        if(head->next != NULL){
            nodePtr = head;
            d=nodePtr->obj;
            head = head->next;
            delete nodePtr;
        }else
            delete head;
        return true;
    }
}

template <class Object>
bool Queue<Object>::isEmpty() const  //Check if Empty
{
    if(!head)
        return true;
    else
        return false;
}

template <class Object>
Queue<Object>::~Queue()   //Destructor
{
    Object temp;
    while (head != NULL)
        dequeue (temp);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心凉 2024-11-06 17:39:46
  1. 不要在标头中包含您的实现文件。包含实现文件中的标头。
  2. 我没有看到你在代码的标题中说“使用命名空间 std”,但你在评论中说你这样做了。不。永远不要在头文件中说using namespace
  3. 将所有模板定义放在标题中。
  4. 您在标头的类定义中缺少分号。
  5. 您的赋值运算符不是异常安全的。确保您的复制构造函数正确,然后使用复制&交换成语。
  6. 您的复制构造函数不正确。如果您想支持惰性复制(写入时复制),那么没问题,但您错过了实际的深层复制操作。使用复制构造函数时要格外小心,因为正确使用它非常重要。
  1. Don't include your implementation file from the header. Include the header from the implementation file.
  2. I don't see you say "using namespace std" in the header in your code, but you say in your comment that you do that. Don't. Never say using namespace in a header file.
  3. Place all template definitions in the header.
  4. You're missing a semicolon in your class definition in the header.
  5. Your assignment operator is not exception-safe. Make sure your copy-constructor is correct and then use the copy & swap idiom.
  6. Your copy-constructor is incorrect. If you want to support lazy copy (copy on write) then that's fine, but you're missing the actual deep copy operation. Be extra-careful with the copy constructor, because it extremely important you get it right.
横笛休吹塞上声 2024-11-06 17:39:46

标题中的类声明后需要一个分号。

class Queue
{
 public:
     Queue();
     Queue(const Queue& a_queue);
     Queue& operator =(const Queue& rhs);
     bool enqueue(const Object& d);
     bool dequeue(const Object& d);
     bool isEmpty() const;
     ~Queue();

private:
    struct ListNode
    {
        Object obj;
        ListNode *next;

    };
    ListNode *head;
};

You need a semicolon after your class declaration in the header.

class Queue
{
 public:
     Queue();
     Queue(const Queue& a_queue);
     Queue& operator =(const Queue& rhs);
     bool enqueue(const Object& d);
     bool dequeue(const Object& d);
     bool isEmpty() const;
     ~Queue();

private:
    struct ListNode
    {
        Object obj;
        ListNode *next;

    };
    ListNode *head;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文