编辑:此问题需要修改,请停止阅读。不要浪费你的时间!谢谢
我在做High School Turbo C ++。我尝试制作一个包含搜索二进制文件的函数的头文件。 我的Headerfile程序是: alpha.h
#ifndef ALPHA_H
#define ALPHA_H
#if !defined __FSTREAM_H
#include<fstream.h>
#endif
#if !defined __PROCESS_H
#include<process.h>
#endif
void searchclass(char* & buf, int, char *);
#endif
根据我在互联网上做的一些研究,我发现定义将放在一个单独的程序中,而不是在主头文件中。所以这就是该计划: ALPHA.CPP
#include<process.h>
#include<fstream.h>
#include"alpha.h"
//All the Definations of the alpha.h header file go here
void searchclass(char* & buf, int s_var, char * file)
{
ifstream fin;
fin.open(file, ios::binary);
if(!fin)
{
cout<<"Error 404: File not found";
exit(-1);
}
while(!fin.read((char*)buf, sizeof(buf)))
if(buf.getint()==s_var)
cout<<"\n\nRecord Found!";
buf.show();
fin.close();
}
请记住,我正在尝试编写一个可以帮助我搜索随机二进制文件的函数,该文件以某种特定的 int变量的类的形式存储记录。所以它应该能够接收任何类的对象并在其中执行搜索。
这是我编写的用于检查头文件的程序。的 A_TEST.CPP
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include"alpha.h"
#include<string.h>
class stu
{ int rn;
char name[20];
public:
void show() //Display Function
{
cout<<"\n\tStudent Details:";
cout<<"\nName: "<<name;
cout<<"\nRoll No.: "<<rn;
}
stu() //constructor
{
rn = 6;
strcpy(name,"Random Name");
}
int getint() //return function
{
return rn;
}
};
char* returnfile()
{ char file[10];
strcpy(file,"file.dat");
return file;
}
void main()
{
clrscr();
int search_var=6;
stu S1;
char file[10];
strcpy(file, "test.dat");
ofstream fout;
fout.open(file, ios::binary);
fout.write((char*)&S1, sizeof(S1));
fout.close();
searchclass((char*)& S1, search_var, file);
getch();
}
在编译A_TEST.CPP(上面的程序)时,我收到警告:
警告A_TEST.CPP 45:临时用于调用参数'buf' 'searchclass(char *&amp;,int,char *)'
在链接时,它给了我这个错误:
链接A_TEST.EXE
链接器错误:未定义的符号搜索类( near&amp;,int,char附近的char 在模块A_TEST.CPP附近)
我不认为ALPHA.CPP文件与alpha.h文件链接,如果我编译ALPHA.CPP文件,它会给我以下错误:
错误ALPHA.CPP 17:左侧所需的结构。或。*
错误ALPHA.CPP 19:左侧所需的结构。或。*
警告ALPHA.CPP 21:从不使用参数's_var'
答案 0 :(得分:0)
在ALPHA.CPP 17和19中,你不能编写这样的代码,因为int类型没有getint()或show()方法。如果要在缓冲区中检查int,则应将指针转换为&#34; int *&#34;首先,尝试以下代码:
int count = fin.read((char*)buf, sizeof(buf));
for (int i = 0; i<(count-4); ++i) {
if (*(int *)(buf + i) == s_var) {
cout << "\n\nRecord Found!";
break;
}
}