草庐IT

C++ 模板 "class type"错误

coder 2024-02-05 原文

我一直在研究一个链表模板类来对各种变量做同样的事情,并设法解决了大部分问题。除了编译时,我得到这些:

g++ -Wall -o template_test template_test.cpp
In file included from template_test.cpp:1:0:
LinkedList.h:50:11: error: declaration of ‘class Type’
LinkedList.h:7:11: error:  shadows template parm ‘class Type’
LinkedList.h:51:30: error: invalid use of incomplete type ‘class LinkedList<Type>’
LinkedList.h:8:7: error: declaration of ‘class LinkedList<Type>’
LinkedList.h:56:11: error: declaration of ‘class Type’
LinkedList.h:7:11: error:  shadows template parm ‘class Type’
LinkedList.h:57:51: error: invalid use of incomplete type ‘class LinkedList<Type>’
LinkedList.h:8:7: error: declaration of ‘class LinkedList<Type>’
LinkedList.h:92:11: error: declaration of ‘class Type’
LinkedList.h:7:11: error:  shadows template parm ‘class Type’
LinkedList.h:93:31: error: invalid use of incomplete type ‘class LinkedList<Type>’
LinkedList.h:8:7: error: declaration of ‘class LinkedList<Type>’
LinkedList.h:102:11: error: declaration of ‘class Type’
LinkedList.h:7:11: error:  shadows template parm ‘class Type’
LinkedList.h:103:34: error: invalid use of incomplete type ‘class LinkedList<Type>’
LinkedList.h:8:7: error: declaration of ‘class LinkedList<Type>’
LinkedList.h:119:11: error: declaration of ‘class Type’
LinkedList.h:7:11: error:  shadows template parm ‘class Type’
LinkedList.h:120:48: error: invalid use of incomplete type ‘class LinkedList<Type>’
LinkedList.h:8:7: error: declaration of ‘class LinkedList<Type>’
LinkedList.h:158:11: error: declaration of ‘class Type’
LinkedList.h:7:11: error:  shadows template parm ‘class Type’
LinkedList.h:159:37: error: invalid use of incomplete type ‘class LinkedList<Type>’
LinkedList.h:8:7: error: declaration of ‘class LinkedList<Type>’
LinkedList.h:193:11: error: declaration of ‘class Type’
LinkedList.h:7:11: error:  shadows template parm ‘class Type’
LinkedList.h:194:34: error: invalid use of incomplete type ‘class LinkedList<Type>’
LinkedList.h:8:7: error: declaration of ‘class LinkedList<Type>’
LinkedList.h:210:11: error: declaration of ‘class Type’
LinkedList.h:7:11: error:  shadows template parm ‘class Type’
LinkedList.h:211:34: error: invalid use of incomplete type ‘class LinkedList<Type>’
LinkedList.h:8:7: error: declaration of ‘class LinkedList<Type>’
make: *** [template_test] Error 1

对于我拥有的每个模板函数,它几乎都说了同样的话。任何人都可以为我解读这些消息的含义,是什么导致编译器抛出这些消息,我该如何解决这个问题?我会重新阅读,并可能同时重写我的代码。
我的代码如下:

#include <string>
#include <iostream>
using namespace std;

template <class Type>
class LinkedList{
public:

 * default constructor and a copy constructor that creates a copy
 * of the given object*/
LinkedList(); //default construtor
LinkedList(const LinkedList& lst); //copy constructor
~LinkedList();//destructor

void add(Type data);

void insertAt(int pos, Type data);

bool remove(Type data );

void removeAll();

void printList();

template <class Type>
LinkedList<Type>::LinkedList(){
head = NULL;
size = 0; //Don't forget to do this!!!
}

template <class Type>
LinkedList<Type>::LinkedList(const LinkedList& lst){
if (lst.head == NULL){
    head = NULL;
    size = 0;
}
else{
    head = new Node;
    head->data = lst.head->data;
    Node *pNewNode = head;
    Node *pOldNode = lst.head->next;
    while (pOldNode != NULL){
        pNewNode->next = new Node;
        pNewNode = pNewNode->next;
        pNewNode->data = pOldNode->data;;
        pOldNode = pOldNode->next;
    }
    pNewNode->next = NULL;
    size = lst.size;
}
}

template <class Type>
LinkedList<Type>::~LinkedList(){
    removeAll();
}

template <class Type>
void LinkedList<Type>::add(Type x){
Node *p = new Node; //temporary node
// Assign appropriate values to the new node
p -> data = x;
p -> next = head;
// Make the head point to the new node
head = p;   
size++;
}

template <class Type>
void LinkedList<Type>::insertAt(int pos, Type x){
Node *p;
Node *newNode;

// Check that pos is a valid index
if (pos <= size){
    newNode = new Node; //new node
    newNode->data = x;

    // Deal with case when item is to be inserted at the head of the list
    if (pos == 0){
        newNode->next = head;
        head = newNode;
    }// if(pos == 0)
    else{
        p = head;
        // Move to position BEFORE insertion point
        for(int i = 0; i < pos-1; i++){
            // Check that p points to a node
            // Note using exception handling should throw an exception         
            if(p == NULL){
                return;
            }
            p = p->next;
        }//for
        // Insert node
        newNode->next = p->next;
        p->next = newNode;
    }//else (pos != 0)
    size++;
}//else (pos >= size) do nothing
}

template <class Type>
bool LinkedList<Type>::remove(Type x){
Node *p = head;
Node *temp;
if (head == NULL){
    return false;
}
else if (head->data == x){
    head = head->next;
    delete p; //currently assigned head
    size--;
    return true;
}
else{
    while(p->next != NULL){
        // Check next node for target
        if(p->next->data == x){
            temp = p->next;
            p->next = p->next->next;
            delete temp;
            return true;
        }
        p = p->next;
    }
}
return false;
}

template <class Type>
void LinkedList<Type>::removeAll(){
Node *p = head;
// Traverse the list deleting nodes
while (p!= NULL){
    head = head->next; // Mustn't "lose" the next node
    delete p; // De-allocate memory
    p = head; // Go to next node
}
head = NULL;
size = 0;
}

template <class Type>
void LinkedList<Type>::printList(){
Node *p = head;
cout << "["; //Nice format!
// Traverse the list
while (p != NULL){
    cout << p -> data; // Print data
    p = p -> next; // Go to next node
    if (p != NULL){
        cout << ","; // Print a comma unless at the end of the list
    }
}
cout << "]"; // Don't print a newline - it might not be wanted
}


private:
struct Node {
    Type data; //list data
    Node *next; //pointer to next item in the list
};

Node *head; //Pointer to the first node in the list
int size; //Records the number of nodes in the list
};

最佳答案

您正在使用语法在类内部的范围之外定义成员函数。这行不通。

应该这样做:

template<typename X>
class Foo {
public:
  void f();
}; // notice end of class scope here

template<typename X>
void Foo<X>::f() {
  // impl
}

关于C++ 模板 "class type"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13522232/

有关C++ 模板 "class type"错误的更多相关文章

  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-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

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

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

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

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

  6. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  7. 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

  8. 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

  9. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  10. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

随机推荐