gpt4 book ai didi

c++ - C++ 中的 LNK2005 和 LNK1169 错误

转载 作者:搜寻专家 更新时间:2023-10-31 02:12:47 24 4
gpt4 key购买 nike

我打算用 C++ 开发 bptree。我是 C++ 编程的新手,遇到了这个错误,我不确定它们是什么。 enter image description here

我有四个文件:节点.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

节点.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 文件。但仍然得到这个错误。如果有人对如何解决这个问题有任何建议,我真的很感激。

最佳答案

BpTree.h 包含 Node.cpp。这是将该文件中的所有符号提取到#includes BpTree.h 的任何文件中。

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

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

关于c++ - C++ 中的 LNK2005 和 LNK1169 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42311354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com