【问题标题】:C++ Generic Linked ListC++ 通用链表
【发布时间】:2011-01-16 13:55:42
【问题描述】:

下面的代码:

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

我正在尝试实现一个通用链表,我意识到我可以导入 &lt;list&gt; 但这不适合我的项目。我正在尝试创建 Foo2Foo3 对象的链接列表 - 以上是我能完成的最好的,因为我是 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â

【问题讨论】:

  • 您遇到了什么问题?
  • 家庭作业?如果没有,为什么不使用std::list
  • 为什么std::list 不能满足您的需求?
  • 不是功课——链表很简单,但不是通用链表。
  • std::list 不能“满足您的需求”是荒谬的。它是一个实现良好的链表。如果你需要一个好的链表,那就是你应该使用的。如果你需要别的东西,那么尝试编写你自己的链表就没有什么意义了。很抱歉,std::list 有 99.9% 的可能性是您需要的。最后的 0.01% 是为了覆盖它不是的可能性,然后你自己的实现也不会工作

标签: c++ templates generics linked-list


【解决方案1】:

您需要使用 T*,而不是 T。在我看来,您来自 Java,一切都是参考。 C++ 模板中没有?。我认为你需要先拿起一本关于基本 C++ 的书,然后再回到模板。

【讨论】:

  • “?”是表示我不知道应该有什么。
  • @Kay:哦,好的。我以为您的意思是 Java 的 ?,这是一个有效的通用参数。但是,您的代码有很多错误,您需要一本书。
【解决方案2】:

我认为问题是“?”在链表中

如果是这种情况,那么您应该使用LinkedList&lt;Foo1 *&gt;

为什么不能使用 std::list?也许我们可以帮助您,使用您自己的实现会好得多。

【讨论】:

  • 我不能使用std::list,因为我想创建一个LinkedList,它的下一个节点有多种可能的选择——它随机选择一个。
  • 我明白了,使用 std::list 怎么样?这可以让您使用 std::list 并为您提供一组“选择”。我认为最好不要考虑列表,而是专注于抽象您的问题。
  • MultipleChoice ?为什么不简单地std::list&lt;boost::variant&lt;Foo2, Foo3&gt; &gt;
【解决方案3】:

尽管你的断言相反,你给出的例子可以用std::list解决:

std::list<Foo1 *> list;

list.push_back(new Foo2());
list.push_back(new Foo3());

for (std::iterator<Foo1 *> it = list.begin(); it != list.end(); ++it)
{
    (*it)->print();
}

显然,这里存在潜在的内存泄漏......

【讨论】:

    【解决方案4】:

    结合这些位,看起来应该可以:

    int main() {
      std::list<boost::variant<Foo2, Foo3> > list;
      list.push_back(Foo2());
      list.push_back(Foo3());
      printAll(list); // You'd still need to write this obviously.
      return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多