图书管理程序有个功能无法实现
各位大侠,按照书上代码在vs2010下面进行图书管理程序。已经能够分段调试,可以实现两个功能。但是第三个功能调试时发现报出以下错误。
1>mainprogram_183.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall CBook::DeleteData(int)" (?DeleteData@CBook@@QAEXH@Z),该符号在函数 "void __cdecl DeleteBook(void)" (?DeleteBook@@YAXXZ) 中被引用
1>D:\My Documents\Visual Studio 2010\Projects\cbook_18\Debug\cbook_18.exe : fatal error LNK1120: 1 个无法解析的外部命令
下面分别把我程序中用的三个代码放在这里,希望能够给与解决。如果这个问题解决后,我将把自己这段代码放到此论坛进行共享,以帮助像我这样的初学者。谢谢。
book.h
#define NUM1 128
#define NUM2 50
class CBook
{
public:
CBook(){}
CBook(char* cName,char* clsbn,char* cPrice,char* cAuthor);
~CBook(){}
public:
char* GetName();
void SetName(char* cName);
char* Getlsbn();
void Setlsbn(char* clsbn);
char* GetPrice();
void SetPrice(char* cPrice);
char* GetAuthor();
void SetAuthor(char* cAuthor);
void WriteData();
void DeleteData(int iCount);
//void GetBookFromFile(int iCount);
protected:
char m_cName[NUM1];
char m_clsbn[NUM1];
char m_cPrice[NUM2];
char m_cAuthor[NUM2];
};
book.cpp
#include "StdAfx.h"
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include"Book.h"
using namespace std;
CBook::CBook(char* cName,char* clsbn,char* cPrice,char* cAuthor)
{
strncpy_s(m_cName,cName,NUM1);
strncpy_s(m_clsbn,clsbn,NUM1);
strncpy_s(m_cPrice,cPrice,NUM2);
strncpy_s(m_cAuthor,cAuthor,NUM2);
}
char* CBook::GetName()
{
return m_cName;
}
void CBook::SetName(char* cName)
{
strncpy_s(m_cName,cName,NUM1);
}
char* CBook::Getlsbn()
{
return m_clsbn;
}
void CBook::Setlsbn(char* clsbn)
{
strncpy_s(m_clsbn,clsbn,NUM1);
}
char* CBook::GetPrice()
{
return m_cPrice;
}
void CBook::SetPrice(char* cPrice)
{
strncpy_s(m_cPrice,cPrice,NUM2);
}
char* CBook::GetAuthor()
{
return m_cAuthor;
}
void CBook::SetAuthor(char* cAuthor)
{
strncpy_s(m_cAuthor,cAuthor,NUM2);
}
void CBook::WriteData()
{
ofstream ofile;
ofile.open("book.dat",ios::binary|ios::app);
try
{
ofile.write(m_cName,NUM1);
ofile.write(m_clsbn,NUM1);
ofile.write(m_cPrice,NUM2);
ofile.write(m_cAuthor,NUM2);
}
catch(...)
{
throw"file error occured";
ofile.close();
}
ofile.close();
}
/*void CBook::GetBookFromFile(int iCount)
{
char cName[NUM1];
char clsbn[NUM1];
char cPrice[NUM2];
char cAuthor[NUM2];
ifstream ifile;
ifile.open("book.dat",ios::binary);
try
{
ifile.seekg(iCount*(NUM1+NUM1+NUM2+NUM2),ios::beg);
ifile.read(cName,NUM1);
if(ifile.tellg()>0)
strncpy(m_cName,cName,NUM1);
ifile.read(cPrice,NUM2);
if(ifile.tellg()>0)
strncpy(m_clsbn,clsbn,NUM1);
ifile.read(cAuthor,NUM2);
if(ifile.tellg()>0)
strncpy(m_cAuthor,cAuthor,NUM2);
}
catch(...)
{
throw"file error occured";
ifile.close();
}
ifile.close();
}*/
void DeleteData(int iCount)
{
long respos;
int iDataCount=0;
fstream file;
fstream tmpfile;
ofstream ofile;
char cTempBuf[NUM1+NUM1+NUM2+NUM2];
file.open("book.dat",ios::binary|ios::in|ios::out|ios::trunc);
file.seekg(0,ios::end);
respos=file.tellg();
iDataCount=respos/(NUM1+NUM1+NUM2+NUM2);
if(iCount<0&&iCount>iDataCount)
{
throw "input number error";
}
else
{
file.seekg((iCount)*(NUM1+NUM1+NUM2+NUM2),ios::beg);
for(int j=0;j<(iDataCount-iCount);j++)
{
memset(cTempBuf,0,NUM1+NUM1+NUM2+NUM2);
file.read(cTempBuf,NUM1+NUM1+NUM2+NUM2);
tmpfile.write(cTempBuf,NUM1+NUM1+NUM2+NUM2);
}
file.close();
tmpfile.seekg(0,ios::beg);
ofile.open("book.dat");
ofile.seekp((iCount-1)*(NUM1+NUM1+NUM2+NUM2),ios::beg);
for(int i=0;i<(iDataCount-iCount);i++)
{
memset(cTempBuf,0,NUM1+NUM1+NUM2+NUM2);
tmpfile.read(cTempBuf,NUM1+NUM1+NUM2+NUM2);
ofile.write(cTempBuf,NUM1+NUM1+NUM2+NUM2);
}
}
tmpfile.close();
ofile.close();
remove("temp.data");
}