user1836957
user1836957

Reputation: 547

LNK2005 and LNK1169 error in c++

I intend to develop bptree in c++. I am very new in c++ programming and got this error which I am not sure what they are.enter image description here

I have four files: 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;
}

what is it that I am missing here. I tried to include .h file or .cpp files only once. But still get this error. If any one has any suggestion on how to fix this I really appreciate it.

Upvotes: 2

Views: 226

Answers (1)

Caleb
Caleb

Reputation: 1173

BpTree.h is including Node.cpp. This is pulling all the symbols from that file into any file that #includes BpTree.h.

So when you link, if you link in Node.obj as well as BpTree.obj, you will get duplicate symbol errors like you are now.

The general rule is that you never #include .cpp files - only the header files themselves. This should resolve your issue.

Upvotes: 2

Related Questions