2017-02-18 63 views
2

我打算在C++中开发bptree。我在C++编程中很新,并且遇到了这个错误,我不确定它们是什么。 enter image description hereLNK2005和LNK1169错误在c + +

我有四个文件: Node.h

#ifndef NODE_HEADER 
#define NODE_HEADER 
#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 
public: 
    Node(int order); 
    void printNodeContent(Node* node); 
    friend class BpTree; 
}; 
#endif 

Node.cpp

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


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

    void Node::printNodeContent(Node* node) { 
     map<int,string> values = node->value; 
     for (auto pval : values) { 
      cout << pval.first << "," ; 
     } 
     cout << endl; 
    } 

    void main() { 
    } 

BpTree.h

#ifndef BPTREE_HEADER 
#define BPTREE_HEADER 
#include "Node.cpp" 

class BpTree 
{ 
    Node *root; 
    Node *np; 
    Node *x; 

public: 
    int order; 
    BpTree(); 
    ~BpTree(); 
    BpTree(int ord); 
    int getOrder(); 
    Node* getRoot(); 
    bool insert(int key, string value); 

}; 
#endif 

BpTree.cpp

#include<stdio.h> 
#include<conio.h> 
#include<iostream> 
#include<string> 
#include "BpTree.h" 

BpTree::BpTree(int ord) 
{ 
    root = new Node(ord); 
    order = ord; 
    np = NULL; 
    x = root; 
} 
BpTree::BpTree() 
{ 
} 
BpTree::~BpTree() 
{ 
} 

int BpTree::getOrder() 
{ 
    return order; 
} 
Node* BpTree::getRoot() 
{ 
    return root; 
} 

bool BpTree::insert(int key, string value) { 
    int order = getOrder(); 
    if (x == NULL) { 
     x = root; 
    } 
    else { 
     if (x->leaf && x->keyCount <= order) { 
      x->value.insert({ key, value }); 
     } 

    } 
    return false; 
} 

这是什么,我在这里失踪。我试图只包含.h文件或.cpp文件一次。但仍然得到这个错误。如果任何人有任何建议如何解决这个问题,我真的很感激它。

+2

您不应该在BpTree.h文件中包含Node.cpp include Node.h。 – Rob

回答

2

BpTree.h包含Node.cpp。这是将该文件中的所有符号拖入任何包含BpTree.h的文件。

所以,当你链接,如果你在Node.obj和BpTree.obj中链接,你会得到像你现在一样的重复符号错误。

一般规则是,你永远不会#include .cpp文件 - 只有头文件本身。这应该可以解决您的问题。

+0

我在问题中遇到了同样的问题,即使代码来自Global.h,问题仍然指向我的main.obj。 Global.h正在引发问题,但我不知道为什么?它没有.cpp文件 –

+0

文件的扩展名在这里是不相关的。如果Global.h定义了一个符号并且多个文件包含Global.h,那么所有这些文件都将包含该符号定义。 – Caleb