【发布时间】:2020-06-22 17:21:08
【问题描述】:
我有一个简单的 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++ 新手,我知道编译器认为我在某处重新定义类 Node,但这是在哪里,所以我可以删除定义?
【问题讨论】:
-
将
#pragma once放在每个头文件的开头。
标签: c++ linker linker-errors