如何修复C ++中的多个定义错误
how to fix multiple definition error in c++?
>我试图查看其他相关帖子,但我被困住了
我的头文件看起来像这样
Node.hpp
#include<iostream>
using namespace std;
#ifndef NODE_HPP
#define NODE_HPP
struct Node
{
int value;
Node *start;
Node *end;
}
*start, *end;
int count= 0;
#endif
和
队列.hpp
#include<iostream>
using namespace std;
#ifndef QUEUE_HPP
#define QUEUE_HPP
#include "Node.hpp"
class Queue{
public:
Node *nNode(int value);
void add(int value);
void remove();
void display();
void firstItem();
Queue()
{
start = NULL;
end = NULL;
}
};
#endif
我的队列实现看起来像
#include<iostream>
using namespace std;
#include "Queue.hpp"
#include<cstdio>
#include<cstdlib>
主要看起来像
#include<iostream>
using namespace std;
#include "Queue.hpp"
#include<cstdio>
#include<cstdlib>
我收到以下错误
/tmp/ccPGEDzG.o:(.bss+0x0): multiple definition of `start'
/tmp/ccJSCU8M.o:(.bss+0x0): first defined here
/tmp/ccPGEDzG.o:(.bss+0x8): multiple definition of `end'
/tmp/ccJSCU8M.o:(.bss+0x8): first defined here
/tmp/ccPGEDzG.o:(.bss+0x10): multiple definition of `count'
/tmp/ccJSCU8M.o:(.bss+0x10): first defined here
我在这里做错了什么?
不要在头文件中定义全局变量,将声明和定义拆分为头文件和嵌入文件。如
在头文件 ( Node.hpp
)
extern Node *start;
extern Node *end;
extern int count;
在安装文件中(我认为最好在这里做一个Node.cpp
)
Node *start;
Node *end;
int count = 0;
其他人已经解释了错误的原因:您在多个翻译单元中定义相同的全局变量。
可能的解决方法是仅在一个点中定义它们,并在头文件中将它们声明为extern
。
然而,在我看来,真正的问题是:你真的需要这些全局变量吗?如果它们是队列对象状态的一部分,我们应该使它们成为实例变量。
class Queue{
public:
Node *nNode(int value);
void add(int value);
void remove();
void display();
void firstItem();
Queue()
{
start = NULL;
end = NULL;
}
Node *start, *end; // <----
};
通过这种方式,我们可以在运行时使用多个队列对象,每个对象都将管理自己的数据。相比之下,实例化原始类的许多对象是无用的,因为它们都将在同一个队列上运行。
TL;DR:封装你的状态,避免全局。
在头文件中定义变量 start
、 end
和 count
。这意味着包含该头文件的每个源文件都将在其翻译单元中定义这些变量
仅当需要将这些变量全局化时,才应在头文件中声明它们,然后在单个源文件中定义它们。
要在头文件中声明变量,请将变量标记为extern
:
extern struct Node *start, *end;
extern int count;
您在头文件和主文件中定义了start
、count
和end
,这导致了多重定义错误。
***how to fix multiple definition of 'dictionaryArrayAdd' error in c*** ?
typedef struct{
int prefix; // prefix for byte > 255
int character; // the last byte of the string
} DictElement;
void dictionaryArrayAdd(int prefix, int character, int value);
int dictionaryArrayPrefix(int value);
int dictionaryArrayCharacter(int value);
DictElement dictionaryArray[4095];
// add prefix + character to the dictionary`enter code here`
void dictionaryArrayAdd(int prefix, int character, int value) {
dictionaryArray[value].prefix = prefix;
dictionaryArray[value].character = character;
}
int dictionaryArrayPrefix(int value) {
return dictionaryArray[value].prefix;
}
int dictionaryArrayCharacter(int value) {
return dictionaryArray[value].character;
}
------------------------------------------------------------------------
相关文章:
- 使用命名空间时出现多个定义错误
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 链接阶段出现多重定义错误
- 避免模板类中的重定义错误
- 即使我没有包含多个文件,C++中的多个定义错误
- 跨多个类的全局变量而不会出现重定义错误?
- 尝试运行 wasm 函数时出现模块未定义错误
- C++ 预期的左大括号以及重定义错误
- C++:成员的越界声明必须是纯虚函数的定义错误
- c++中数组的未定义错误
- Visual C:模板类中的自定义错误消息
- 为什么C++模板不会导致多个定义错误?
- 只有一个定义/声明时标头声明变量的多堆定义错误
- C++ 在多个其他类中使用单个类 - 编译时出现多个定义错误
- 基类未定义.错误 C2504
- Q 斯坦达项重定义错误
- 可视化C++中的结构定义错误
- VC++ C2011 重定义错误 - 未使用的头文件