编译源代码时出现多重定义错误
getting multiple definition error when compiling source code
我有一个简单的 C++ 应用程序:
node.h:
#include<iostream>
using namespace::std;
class Node
{
private:
int data;
Node *next;
public:
Node(int nodeData,Node *nextNode);
};
node.cpp:
#include "node.h"
Node::Node(int nodeData, Node *nextNode) {
data = nodeData;
next = nextNode;
}
linked_list.h
#include "node.h"
class LinkedList
{
private:
Node *head;
Node *tail;
int size;
public:
LinkedList();
int getSize();
};
linked_list.cpp:
#include "linked_list.h"
LinkedList::LinkedList()
{
size = 0;
}
int LinkedList::getSize() {
return size;
}
main.cpp:
#include <iostream>
#include "node.h"
#include "linked_list.h"
using namespace ::std;
int main()
{
cout << "This is main!\n";
return 0;
}
我在 linux 上,在 projcet 的目录中,我在那里打开一个终端并尝试通过此命令编译它们:
g++ *.cpp *.h -o app
但是我得到这个错误:
In file included from linked_list.h:1:0,
from main.cpp:3:
node.h:1:7: error: redefinition of ‘class Node’
class Node
^~~~
In file included from main.cpp:2:0:
node.h:1:7: note: previous definition of ‘class Node’
class Node
^~~~
我在 stackoverlfow 上查看了一些帖子,但没有解决我的问题。我是 c++ 的新手,我知道编译器认为我在某处重新定义了 class Node
,但是这在哪里,所以我可以删除定义?
你的linked_list.h
包含了node.h
,所以编译器在编译main.cpp
时会看到两次node.h
中的定义。
为避免此问题,您应该在 header 文件中添加“include guard”。
应该是这样的:
node.h:
#ifndef NODE_H_GUARD // add this
#define NODE_H_GUARD // add this
#include<iostream>
using namespace::std;
class Node
{
private:
int data;
Node *next;
public:
Node(int nodeData,Node *nextNode);
};
#endif // add this
要定义和检查的宏名称在每个 header 中应该不同。
另一种避免此问题的方法是添加 #pragma once
作为 header 的第一行,如果您的编译器支持的话。
我有一个简单的 C++ 应用程序:
node.h:
#include<iostream>
using namespace::std;
class Node
{
private:
int data;
Node *next;
public:
Node(int nodeData,Node *nextNode);
};
node.cpp:
#include "node.h"
Node::Node(int nodeData, Node *nextNode) {
data = nodeData;
next = nextNode;
}
linked_list.h
#include "node.h"
class LinkedList
{
private:
Node *head;
Node *tail;
int size;
public:
LinkedList();
int getSize();
};
linked_list.cpp:
#include "linked_list.h"
LinkedList::LinkedList()
{
size = 0;
}
int LinkedList::getSize() {
return size;
}
main.cpp:
#include <iostream>
#include "node.h"
#include "linked_list.h"
using namespace ::std;
int main()
{
cout << "This is main!\n";
return 0;
}
我在 linux 上,在 projcet 的目录中,我在那里打开一个终端并尝试通过此命令编译它们:
g++ *.cpp *.h -o app
但是我得到这个错误:
In file included from linked_list.h:1:0,
from main.cpp:3:
node.h:1:7: error: redefinition of ‘class Node’
class Node
^~~~
In file included from main.cpp:2:0:
node.h:1:7: note: previous definition of ‘class Node’
class Node
^~~~
我在 stackoverlfow 上查看了一些帖子,但没有解决我的问题。我是 c++ 的新手,我知道编译器认为我在某处重新定义了 class Node
,但是这在哪里,所以我可以删除定义?
你的linked_list.h
包含了node.h
,所以编译器在编译main.cpp
时会看到两次node.h
中的定义。
为避免此问题,您应该在 header 文件中添加“include guard”。
应该是这样的:
node.h:
#ifndef NODE_H_GUARD // add this
#define NODE_H_GUARD // add this
#include<iostream>
using namespace::std;
class Node
{
private:
int data;
Node *next;
public:
Node(int nodeData,Node *nextNode);
};
#endif // add this
要定义和检查的宏名称在每个 header 中应该不同。
另一种避免此问题的方法是添加 #pragma once
作为 header 的第一行,如果您的编译器支持的话。