0

我正在尝试在节点类中初始化一个节点数组,它们是私有成员:

    #include<iostream>
    #include<string>
    using namespace std;
    class Data
    {
    public:
        long data;
        Data(long dd)
        {
            data=dd;
        }
        void displaydata()
        {
            cout<<data <<"  "<<endl;
        }
    };
    class node
    {
//these are what i have changed
        const static int order=4;
        int numitems;
        node *parent;


    vector<node>childarray(order);
        vector<Data>itemarray(order-1);

    };

但不幸的是它没有编译,错误是:

  1>c:\users\dato\documents\visual studio 2010\projects\234_tree\234_tree\234_tree.cpp(26): error C2061: syntax error : identifier 'order'
    1>c:\users\dato\documents\visual studio 2010\projects\234_tree\234_tree\234_tree.cpp(27): error C2061: syntax error : identifier 'order'
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

从下面的java代码。

class DataItem
{
public long dData; // one data item
//--------------------------------------------------------------
public DataItem(long dd) // constructor
{ dData = dd; }
//--------------------------------------------------------------
public void displayItem() // display item, format “/27”
{ System.out.print(“/”+dData); }
//--------------------------------------------------------------
} // end class DataItem
////////////////////////////////////////////////////////////////
class Node
{
private static final int ORDER = 4;
private int numItems;
private Node parent;
private Node childArray[] = new Node[ORDER];
private DataItem itemArray[] = new DataItem[ORDER-1];
// -------------------------------------------------------------
// connect child to this node
public void connectChild(int childNum, Node child)
{
childArray[childNum] = child;
if(child != null)
child.parent = this;
}
// -------------------------------------------------------------
// disconnect child from this node, return it
public Node disconnectChild(int childNum)
{
Node tempNode = childArray[childNum];
childArray[childNum] = null;
return tempNode;
}
// -------------------------------------------------------------
public Node getChild(int childNum)
{ return childArray[childNum]; }
// -------------------------------------------------------------
public Node getParent()
{ return parent; }
// -------------------------------------------------------------
public boolean isLeaf()
Java Code for a 2-3-4 Tree 479
LISTING 10.1 Continued
{ return (childArray[0]==null) ? true : false; }
// -------------------------------------------------------------
public int getNumItems()
{ return numItems; }
// -------------------------------------------------------------
public DataItem getItem(int index) // get DataItem at index
{ return itemArray[index]; }
// -------------------------------------------------------------
public boolean isFull()
{ return (numItems==ORDER-1) ? true : false; }
// -------------------------------------------------------------
public int findItem(long key) // return index of
{ // item (within node)
for(int j=0; j<ORDER-1; j++) // if found,
{ // otherwise,
if(itemArray[j] == null) // return -1
break;
else if(itemArray[j].dData == key)
return j;
}
return -1;
} // end findItem
// -------------------------------------------------------------
public int insertItem(DataItem newItem)
{
// assumes node is not full
numItems++; // will add new item
long newKey = newItem.dData; // key of new item
for(int j=ORDER-2; j>=0; j--) // start on right,
{ // examine items
if(itemArray[j] == null) // if item null,
continue; // go left one cell
else // not null,
{ // get its key
long itsKey = itemArray[j].dData;
if(newKey < itsKey) // if it’s bigger
itemArray[j+1] = itemArray[j]; // shift it right
else
{
itemArray[j+1] = newItem; // insert new item

请帮我轻松转换代码,我必须将订单声明为公开吗?我试过了但是没用,我也试过在类外声明顺序,比如 const int node::order=4 但还没有成功,有什么问题?我需要一个包含成员的数组,大小应该是 order 或 4。当我看书时,作者说你在本书中用 C++ 编写 java 代码时需要什么,它说指针,所以我添加了指针,但还没有成功。

4

1 回答 1

1

线条

node *childarray[]=new node[order];
Data *itemarray[]=new Data[order-1];

这里有错误。除了你不能在课堂上这样分配之外,你也有错误的声明。您正在声明 egchildarray是一个指针数组,但是您将它创建为一个简单的数组。将声明更改为

node *childarray;
Data *itemarray;

要实际进行分配,您必须在构造函数中进行。

但是,我真的建议您改用std::array(或可能std::vector)。

编辑:使用std::array

struct node
{
    static const int ORDER = 4;
    int numitems;
    node* parent;
    std::array<node*, ORDER> childarray;
    std::array<Data*, ORDER - 1> itemarray;

    node()
        : numitems{0},
          parent{nullptr},
          childarray{{nullptr}},
          itemarray{{nullptr}}
        { }
};

现在childarray和是具有和(分别)数量的指针itemarray的数组,全部初始化为(现在应该是什么)。ORDERORDER - 1nullptrNULL

于 2012-09-07T06:17:40.317 回答