草庐IT

c++ - "expected unqualified-id"上的 "using"编译失败

coder 2024-02-16 原文

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

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

想知道是否有人可以帮我解决这个问题。一点点搜索表明问题似乎出在#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);
}

最佳答案

  1. 不要在标题中包含您的实现文件。包括实现文件中的 header 。
  2. 我没有看到您在代码的 header 中说“使用 namespace 标准”,但您在评论中说您这样做了。不。永远不要在头文件中说using namespace
  3. 将所有模板定义放在标题中。
  4. 您在 header 中的类定义中缺少分号。
  5. 您的赋值运算符不是异常安全的。确保您的复制构造函数是正确的,然后使用 copy-and-swap 习惯用法。
  6. 您的复制构造函数不正确。如果你想支持惰性复制(写时复制)那很好,但你错过了实际的深度复制操作。对复制构造函数要格外小心,因为正确使用它非常重要。

关于c++ - "expected unqualified-id"上的 "using"编译失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5574732/

有关c++ - "expected unqualified-id"上的 "using"编译失败的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  4. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  5. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  6. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

  7. ruby-on-rails - openshift 上的 rails 控制台 - 2

    我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新ruby​​gems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems

  8. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  9. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  10. ruby - 即使失败也继续进行多主机测试 - 2

    我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r

随机推荐