【问题标题】:Compilation fails with "expected unqualified-id" on "using"编译失败,“使用”时出现“预期的不合格 ID”
【发布时间】:2011-04-07 01:02:30
【问题描述】:

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

queue.cpp:2: 错误:在“使用”之前预期的不合格 ID

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

下面的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

这里是 queue.cpp 文件。

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

【问题讨论】:

  • 再看代码后,是不是因为我包含了“using namespace std;”在我的 .h 和 .cpp 中??

标签: c++ g++ linked-list


【解决方案1】:
  1. 不要在标题中包含您的实现文件。包含实现文件中的标头。
  2. 我没有看到您在代码的标题中说“使用命名空间 std”,但您在评论中说您这样做了。不。永远不要在头文件中说using namespace
  3. 将所有模板定义放在标题中。
  4. 标题中的类定义中缺少分号。
  5. 您的赋值运算符不是异常安全的。确保您的复制构造函数正确,然后使用复制和交换习语。
  6. 您的复制构造函数不正确。如果您想支持惰性复制(写入时复制),那很好,但是您缺少实际的深度复制操作。使用复制构造函数时要格外小心,因为正确处理非常重要。

【讨论】:

  • 她必须包含实现,因为它是一个模板,它不会在大多数编译器上单独编译。要么就是这样,要么把所有东西都放在标题中。
  • 我就是这么说的。将所有内容放在标题中。或者将定义移动到模板实现文件(.tcc 等)。但扩展名 .cpp 用于实现文件,而不用于模板定义。
  • 对不起,我误解了你所说的意思。我犹豫要不要写我的评论,因为我想你知道模板。大声笑。
  • 我认为复制结构是支持软拷贝,而分配是深拷贝?这是假的吗??
  • @kingcong3 在您的代码中,您的复制 ctor 是根据您的赋值运算符实现的。它不执行浅拷贝。但同样,即使是这样,您也需要考虑写入Queue 克隆的情况。在这种情况下,如果副本很浅,两个 Queue 对象都会更改。
【解决方案2】:

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

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

【讨论】:

  • 啊,好棘手,它一直告诉我错误在 .cpp 中,但它确实在 .h 中 非常感谢
猜你喜欢
  • 2016-05-01
  • 2019-03-14
  • 1970-01-01
  • 1970-01-01
  • 2015-02-04
  • 2023-03-02
  • 2020-12-10
  • 2021-08-26
  • 1970-01-01
相关资源
最近更新 更多