繁体   English   中英

C2011“节点”:“类”类型的重新定义

[英]C2011 'Node':'class' type redefinition

我正在使用C ++实现bptree。 我陷入了节点创建的第一步。 不断收到“ C2011'节点':'类'类型重新定义”错误。 我在网上找到了一些建议,可以从cpp文件中删除类关键字。 但是,当我删除class关键字时,会遇到很多其他错误。 这是我的Node.cpp代码:

#include "Node.h"
#include <cstdio>
#include <iostream>
#include <string>
#include <map>


using namespace std;


 class Node {
    bool leaf;
    Node** kids;
    map<int, string> value;
    int  keyCount;//number of current keys in the node

    //constructor;
    Node::Node(int order) {
        this->value = {};
        this->kids = new Node *[order + 1];
        this->leaf = true;
        this->keyCount = 0;
        for (int i = 0; i < (order + 1); i++) {
            this->kids[i] = NULL;
        }           
    }
};

而Node.h文件如下:

#pragma once
#ifndef NODE_HEADER
#define NODE_HEADER

class Node {

public:
    Node(int order) {};
};

#endif

我怎样才能解决这个问题?

问题

在C ++中,只要您#include将标头简单地粘贴到主体中即可。 所以现在编译器看到:

class Node {

public:
   Node(int order) {};
};

// stuff from system headers omitted for brevity

using namespace std;

class Node {
  bool leaf;
  //...
};

这里有两个问题:

  1. 编译器两次看到具有不同主体的class Node

  2. Node::Node被定义了两次(第一次为空{} )。

标头应包含类声明

 #include <map>

 using namespace std;

 class Node {
    bool leaf;
    Node** kids;
    map<int, string> value;
    int keyCount;//number of current keys in the node

    //constructor;
    Node(int order);
};

注意,这里的构造函数没有主体。 这只是一个声明。 因为它使用map您需要在声明之前包含<map>using namespace添加。

之后,不要将class Node再次放入.cpp.cc文件中。 仅将方法实现放在顶层:

Node::Node(int order) {
  // ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM