将数据结构类(节点列表)与另一个类一起使用

时间:2013-12-04 22:43:58

标签: c++ class data-structures

我试图将来自另一个类的信息放入基于节点的列表类中。例如,我有一张类卡,然后我想把那些在数组中的卡放入我的基于节点的列表类中。我只想试着理解语法。我是否需要让我的卡片类成为节点和列表类的朋友。

我总共有2个类(节点和列表),它们将包含来自卡类的信息。我知道我必须模拟节点和列表,我混淆了如何以及为什么?

 ./class.h:5:7: error: redefinition of 'card'
class card {
      ^
./class.h:5:7: note: previous definition is here
class card {
      ^
In file included from project8.cpp:9:
In file included from ./nodallist.cpp:2:
In file included from ./listN.h:4:
In file included from ./class.cpp:3:
./class.h:35:7: error: redefinition of 'player'
class player{
      ^
./class.h:35:7: note: previous definition is here
class player{
      ^
In file included from project8.cpp:9:
./nodallist.cpp:7:7: error: out-of-line definition of 'Node' does not match any
      declaration in 'Node'
Node::Node(card value, Node* nodeyo)
      ^~~~
./nodallist.cpp:9:5: error: assigning to 'char' from incompatible type 'card'
data=value;
    ^~~~~~
./nodallist.cpp:149:12: error: out-of-line definition of 'insertAfter' does not
      match any declaration in 'List'
bool List::insertAfter(card value)
           ^~~~~~~~~~~
./nodallist.cpp:153:18: error: no matching constructor for initialization of
      'Node'
        Node *newd= new Node(value,NULL);
                        ^    ~~~~~~~~~~
./listN.h:12:3: note: candidate constructor not viable: no known conversion from
      'card' to 'char' for 1st argument
                Node(char, Node*);
                ^
./listN.h:10:7: note: candidate constructor (the implicit copy constructor) not
      viable: requires 1 argument, but 2 were provided
class Node{
      ^
In file included from project8.cpp:9:
./nodallist.cpp:160:19: error: no matching constructor for initialization of
      'Node'
        Node *noden= new Node(value,cursor->next);
                         ^    ~~~~~~~~~~~~~~~~~~
./listN.h:12:3: note: candidate constructor not viable: no known conversion from
      'card' to 'char' for 1st argument
                Node(char, Node*);
                ^
./listN.h:10:7: note: candidate constructor (the implicit copy constructor) not
      viable: requires 1 argument, but 2 were provided
class Node{
      ^
In file included from project8.cpp:9:
./nodallist.cpp:167:12: error: out-of-line definition of 'insertBefore' does not
      match any declaration in 'List'
bool List::insertBefore(card value)
           ^~~~~~~~~~~~
./nodallist.cpp:171:19: error: no matching constructor for initialization of
      'Node'
                Node *newd= new Node(value,NULL);
                                ^    ~~~~~~~~~~
./listN.h:12:3: note: candidate constructor not viable: no known conversion from
      'card' to 'char' for 1st argument
                Node(char, Node*);
                ^
./listN.h:10:7: note: candidate constructor (the implicit copy constructor) not
      viable: requires 1 argument, but 2 were provided
class Node{
      ^
In file included from project8.cpp:9:
./nodallist.cpp:180:14: error: assigning to 'char' from incompatible type 'card'
        cursor->data=value;
                    ^~~~~~
./nodallist.cpp:185:12: error: out-of-line definition of 'remove' does not match
      any declaration in 'List'
bool List::remove(card &value)
           ^~~~~~
./nodallist.cpp:225:12: error: out-of-line definition of 'replace' does not
      match any declaration in 'List'
bool List::replace(card value)
           ^~~~~~~
./nodallist.cpp:233:13: error: assigning to 'char' from incompatible type 'card'
cursor->data=value;
            ^~~~~~
./nodallist.cpp:238:12: error: out-of-line definition of 'getCursor' does not
      match any declaration in 'List'
bool List::getCursor(card &value) const
           ^~~~~~~~~
project8.cpp:19:12: error: redefinition of 'SPACE'
const char SPACE = ' ';
           ^
./class.cpp:9:12: note: previous definition is here
const char SPACE= ' ';

2 个答案:

答案 0 :(得分:0)

class card
{
    /* stuff */
};

class node
{
    /* other stuff */;
    card c;
};

如果您想在节点中获取卡的数据,只需将卡放入节点即可。

答案 1 :(得分:0)

这很容易......在类卡中,有两个函数 - 一个用于输入每张卡的信息(使用对象数组的概念)。之后,尝试实现所谓的“使用链接列表排队”。是的,使用模板类:C ++中的Queue,您可以非常轻松地实现它,但您也可以尝试这种方法!祝好运! :)