C语言中重定义 错误

假设有三个文件

node.h //定义节点
list.h //对链表的操作函数
test.c //测试函数
包含关系如下:
list.h中
#include "node.h"
test.c中
#include "list.h"
#include "node.h"
#include ... 省略其它必要的头文件
使用命令编译
$gcc -o test test.c
编译时,会出现错误:XXX重定义
为什么呢?
1)test.c中包含了node.h,因为node.h是定义结构的文件,而且已经被list.h包含了,所以这里node.h会预编译两次,出现重定义!
所以,可以去掉test.c中的头文件node.h即可
2)修改node.h,避免重定义,这种方法也是推荐的方法
#ifndef _NODE
#define _NODE
typedef struct node{
int x;
struct node *next;
}NODE;
#endif
使用一个标记变量_NODE来表示NODE结构已经被定义了,将定义过程包含在#ifndef~#endif中,这样,不管包含多少次node.h文件,都不会出现重定义。
当然,这不仅仅限于结构的定义。
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是遍历链表的.h和.cpp文件的代码,可以使用switch case语句输出: 链表头文件 LinkedList.h: ```c++ #ifndef LINKEDLIST_H #define LINKEDLIST_H #include <iostream> struct Node { int data; Node* next; }; class LinkedList { public: LinkedList(); ~LinkedList(); void insert(int value); void remove(int value); void print(); private: Node* head; }; #endif // LINKEDLIST_H ``` 链表源文件 LinkedList.cpp: ```c++ #include "LinkedList.h" LinkedList::LinkedList() { head = nullptr; } LinkedList::~LinkedList() { Node* current = head; while (current != nullptr) { Node* next = current->next; delete current; current = next; } } void LinkedList::insert(int value) { Node* newNode = new Node; newNode->data = value; newNode->next = head; head = newNode; } void LinkedList::remove(int value) { Node* current = head; Node* previous = nullptr; while (current != nullptr) { if (current->data == value) { if (previous == nullptr) { head = current->next; } else { previous->next = current->next; } delete current; return; } previous = current; current = current->next; } } void LinkedList::print() { Node* current = head; while (current != nullptr) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; } ``` 使用switch case输出链表.h和.cpp文件的代码: ```c++ #include <iostream> #include <fstream> #include <string> int main() { std::ifstream hFile("LinkedList.h"); std::ifstream cppFile("LinkedList.cpp"); std::string line; int fileIndex = 0; while (getline(hFile, line)) { switch (fileIndex) { case 0: std::cout << "// 链表头文件 LinkedList.h:" << std::endl; break; case 1: std::cout << "// 链表源文件 LinkedList.cpp:" << std::endl; break; } std::cout << line << std::endl; if (line.find("#endif") != std::string::npos) { ++fileIndex; } } while (getline(cppFile, line)) { switch (fileIndex) { case 1: std::cout << "// 链表头文件 LinkedList.h:" << std::endl; break; case 2: std::cout << "// 链表源文件 LinkedList.cpp:" << std::endl; break; } std::cout << line << std::endl; if (line.find("void LinkedList::print()") != std::string::npos) { ++fileIndex; } } hFile.close(); cppFile.close(); return 0; } ``` 其,fileIndex变量表示当前正在处理的文件的索引。当读取到`.h`文件的`#endif`预处理指令时,fileIndex加1,表示开始读取`.cpp`文件;当读取到`.cpp`文件的`void LinkedList::print()`函数定义时,fileIndex再次加1,表示`.cpp`文件处理完毕。在switch case语句,根据fileIndex的值输出对应的注释和代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值