- 论坛徽章:
- 0
|
写了一个C++程序 其中有两个源文件include同一个头文件 虽然使用了#ifndef命令 但仍是报错 望大神指点
头文件 my_head1:
void read_data(void);
void write_data(void);
void randd(void);
头文件 my_head2:
#ifndef _my_head2_
#define _my_head2_
#include<fstream>
using namespace std;
ifstream readd;
ofstream writed;
#else
extern ifstream readd;
extern ofstream writed;
#endif
源文件 read_data:
#include<fstream>
#include<iostream>
#include"my_head2.h"
using namespace std;
void read_data()
{
readd.open("datamem.txt");
if(readd==NULL) {cout<<"Read datamem.txt error!"<<endl;exit(0);}
int num;
while(1){
readd>>num;
if(!readd.eof()) cout<<num<<" ";
else break;
}
readd.close();
// readd.clear();
}
源文件write_data:
#include<fstream>
#include<iostream>
#include"my_head2.h"
using namespace std;
void write_data()
{
writed.open("datamem.txt");
if(writed==NULL) {cout<<"Write datamem.txt error!"<<endl;exit(0);}
int i;
for(i=0;i<20;i++)
writed<<i<<" ";
writed.close();
// writed.clear();
}
顶层文件:
#include<stdlib.h>
#include"my_head1.h"
void main()
{
write_data();
read_data();
}
错误提示:
1>write_data.obj : error LNK2005: "class std::basic_ifstream<char,struct std::char_traits<char> > readd" (?readd@@3V?$basic_ifstream@DU?$char_traits@D@std@@@std@@A) 已经在 read_data.obj 中定义
1>write_data.obj : error LNK2005: "class std::basic_ofstream<char,struct std::char_traits<char> > writed" (?writed@@3V?$basic_ofstream@DU?$char_traits@D@std@@@std@@A) 已经在 read_data.obj 中定义
1>C:\Users\Flyingsheep\Desktop\old_program\C++_test\test_include2\test_include\Debug\test_include.exe : fatal error LNK1169: 找到一个或多个多重定义的符号
不知道为什么 望大神指点啊 |
|