<i id='5kQMS'><tr id='5kQMS'><dt id='5kQMS'><q id='5kQMS'><span id='5kQMS'><b id='5kQMS'><form id='5kQMS'><ins id='5kQMS'></ins><ul id='5kQMS'></ul><sub id='5kQMS'></sub></form><legend id='5kQMS'></legend><bdo id='5kQMS'><pre id='5kQMS'><center id='5kQMS'></center></pre></bdo></b><th id='5kQMS'></th></span></q></dt></tr></i><div id='5kQMS'><tfoot id='5kQMS'></tfoot><dl id='5kQMS'><fieldset id='5kQMS'></fieldset></dl></div>
    • <bdo id='5kQMS'></bdo><ul id='5kQMS'></ul>

    <small id='5kQMS'></small><noframes id='5kQMS'>

    1. <legend id='5kQMS'><style id='5kQMS'><dir id='5kQMS'><q id='5kQMS'></q></dir></style></legend>
      <tfoot id='5kQMS'></tfoot>
    2. C++ 通用链表

      时间:2023-10-18
        <tfoot id='vTl5G'></tfoot>

        <i id='vTl5G'><tr id='vTl5G'><dt id='vTl5G'><q id='vTl5G'><span id='vTl5G'><b id='vTl5G'><form id='vTl5G'><ins id='vTl5G'></ins><ul id='vTl5G'></ul><sub id='vTl5G'></sub></form><legend id='vTl5G'></legend><bdo id='vTl5G'><pre id='vTl5G'><center id='vTl5G'></center></pre></bdo></b><th id='vTl5G'></th></span></q></dt></tr></i><div id='vTl5G'><tfoot id='vTl5G'></tfoot><dl id='vTl5G'><fieldset id='vTl5G'></fieldset></dl></div>

              <tbody id='vTl5G'></tbody>
          1. <legend id='vTl5G'><style id='vTl5G'><dir id='vTl5G'><q id='vTl5G'></q></dir></style></legend>
              <bdo id='vTl5G'></bdo><ul id='vTl5G'></ul>

              <small id='vTl5G'></small><noframes id='vTl5G'>

                本文介绍了C++ 通用链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                对于下面的代码:

                #include <iostream>
                #include <string>
                
                using namespace std;
                
                class Foo2;
                class Foo3;
                
                template <class T>
                class Foo1 {
                  public:
                    Foo1();
                    void print() {
                      cout << "My name is: " << name << endl;
                    }
                
                    T getNext(){
                      return nextLink;
                    }
                
                    string name;
                    T nextLink;
                
                };
                
                class Foo2 : public Foo1 {
                  public:
                    Foo2(){
                      name = "Foo2";
                    }
                };
                
                
                class Foo3 : public Foo1 {
                  public:
                    Foo3(){
                      name = "Foo3";
                    }
                };
                
                template <class T>
                class LinkedList {
                
                
                
                public:
                    T curr;
                    T first;
                
                void add(T node){
                  if(first == NULL){
                    first = node
                  }
                  node->nextLink = this;
                  curr = node;
                }
                T getNext(){
                  return next;
                }
                void printAll(){
                  T curr = first;
                  cout << "Contents are: " ;
                  while(curr != NULL){
                    cout << curr.print() << ", ";
                    curr = curr.getNext();
                  }
                }
                
                };
                
                int main() {
                  LinkedList<?> list;
                  list.add(new Foo2());
                  list.add(new Foo3());
                  list.printAll();
                  return 0;
                }
                

                我正在尝试实现一个通用链表,我意识到我可以导入 <list> 但这不适合我的项目.我正在尝试使用 Foo2Foo3 对象的链接列表 - 由于我是 C++ 新手,以上是我能做到的最好的.

                I'm attempting to implement a generic linked list, i realise that i could import <list> but that wouldn't suit my project. I'm trying to have a linked list of Foo2 and Foo3 objects - the above is the best i could accomplish as i'm new to C++.

                错误:

                generic.C: In instantiation of Foo1<Foo2>:
                generic.C:26:   instantiated from here
                generic.C:22: error: Foo1<T>::nextLink has incomplete type
                generic.C:6: error: forward declaration of class Foo2
                generic.C: In instantiation of Foo1<Foo3>:
                generic.C:34:   instantiated from here
                generic.C:22: error: Foo1<T>::nextLink has incomplete type
                generic.C:7: error: forward declaration of class Foo3
                generic.C: In member function void LinkedList<T>::add(T):
                generic.C:50: error: expected ; before } token
                generic.C: In member function T LinkedList<T>::getNext():
                generic.C:55: error: next was not declared in this scope
                generic.C: In function int main():
                generic.C:69: error: template argument 1 is invalid
                generic.C:69: error: invalid type in declaration before ; token
                generic.C:70: error: request for member add in list, which is of non-class type int
                generic.C:71: error: request for member add in list, which is of non-class type int
                generic.C:72: error: request for member printAll in list, which is of non-class type int
                

                推荐答案

                我认为问题是?"在链表中

                I think the problem is the "?" in LinkedList

                如果是这种情况,那么您应该使用 LinkedList.

                If this is the case, then you should use LinkedList<Foo1 *>.

                为什么不能使用 std::list?也许我们可以帮助您解决这个问题,使用您自己的实现会好得多.

                Why can't you use std::list? Maybe we can help you with that, it will be far better that using your own implementation.

                这篇关于C++ 通用链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何删除链表中连续的两项 下一篇:删除链表中的所有节点

                相关文章

                <small id='WmcFe'></small><noframes id='WmcFe'>

                      <bdo id='WmcFe'></bdo><ul id='WmcFe'></ul>
                  1. <tfoot id='WmcFe'></tfoot>

                    <i id='WmcFe'><tr id='WmcFe'><dt id='WmcFe'><q id='WmcFe'><span id='WmcFe'><b id='WmcFe'><form id='WmcFe'><ins id='WmcFe'></ins><ul id='WmcFe'></ul><sub id='WmcFe'></sub></form><legend id='WmcFe'></legend><bdo id='WmcFe'><pre id='WmcFe'><center id='WmcFe'></center></pre></bdo></b><th id='WmcFe'></th></span></q></dt></tr></i><div id='WmcFe'><tfoot id='WmcFe'></tfoot><dl id='WmcFe'><fieldset id='WmcFe'></fieldset></dl></div>
                    <legend id='WmcFe'><style id='WmcFe'><dir id='WmcFe'><q id='WmcFe'></q></dir></style></legend>