源代码:error link问题:
源代码:
一个 aaa.h, aaa.cpp, bbb.cpp(main函数在其中)
组成的工程;
//aaa.h
#ifndef _strct_H
#define _strct_H
typedef unsigned char myBYTE;
typedef unsigned short myWORD;
typedef unsigned long myDWORD;
//------------------------------------------------------
typedef struct mytagBITMAPFILEHEADER
{
myWORD bmpFileType;
myDWORD bmpFileSize;
myWORD bmpFileReserved1, bmpFileReserved2;
myDWORD bmpHeadtoImgDateOffset;
} bmpFILEheader, *P_bmpFILEheader;
//----------------------------------------------------------
typedef struct mytagBITMAPINFOHEADER
{
myDWORD bmpINFOstrctSize;
long bmpWidth, bmpHeight;
myWORD bmpPlanes;
myWORD bmpColorNumDef;
myDWORD bmpCompression;
myDWORD bmpImgSize;
long bmpXPelsPerMeter, bmpYPelsPerMeter;//曋辨率
myDWORD bmpUsedColor, bmpImportantColor;
}bmpINFOheader, *P_bmpINFOheader;
//-----------------------------------------------------------
typedef struct mytagRGBQUAD
{
myBYTE rgbBlue, rgbGreen, rgbRed;
myBYTE rgbReserved;
}rgbDate;
#endif;
//aaa.cpp
#include "aaa.h "
#include <fstream>
#include <string.h>
#include <iostream>
using namespace std;
void writebmpFILEhead(string &infilename, bmpFILEheader & bmpfilehead)
{
ifstream inFile(infilename.c_str(),ios::beg);
inFile.read((char*)(&bmpfilehead.bmpFileType), sizeof(bmpfilehead.bmpFileType));
inFile.read((char*)(&bmpfilehead.bmpFileSize), sizeof(bmpfilehead.bmpFileSize));
inFile.read((char*)(&bmpfilehead.bmpFileReserved1),sizeof(bmpfilehead.bmpFileReserved1));
inFile.read((char*)(&bmpfilehead.bmpFileReserved2),sizeof(bmpfilehead.bmpFileReserved2));
inFile.read((char*)(&bmpfilehead.bmpHeadtoImgDateOffset),sizeof(bmpfilehead.bmpHeadtoImgDateOffset));
inFile.close();
}
void writebmpINFOhead(string &infilename, bmpINFOheader & bmpinfophead)
{
ifstream inFile(infilename.c_str(),ios::beg);
inFile.seekg(14,ios::beg);
inFile.read( (char*)(&bmpinfophead.bmpINFOstrctSize), sizeof(bmpinfophead.bmpINFOstrctSize));
inFile.read((char*)(&bmpinfophead.bmpWidth), sizeof(bmpinfophead.bmpWidth));
inFile.read((char*)(&bmpinfophead.bmpHeight), sizeof(bmpinfophead.bmpHeight));
inFile.read((char*)(&bmpinfophead.bmpPlanes), sizeof(bmpinfophead.bmpPlanes));
inFile.read((char*)(&bmpinfophead.bmpColorNumDef),sizeof(bmpinfophead.bmpColorNumDef));
inFile.read((char*)(&bmpinfophead.bmpCompression), sizeof(bmpinfophead.bmpCompression));
inFile.read((char*)(&bmpinfophead.bmpImgSize), sizeof(bmpinfophead.bmpImgSize));
inFile.read((char*)(&bmpinfophead.bmpXPelsPerMeter),sizeof(bmpinfophead.bmpXPelsPerMeter));
inFile.read((char*)(&bmpinfophead.bmpYPelsPerMeter),sizeof(bmpinfophead.bmpYPelsPerMeter));
inFile.read((char*)(&bmpinfophead.bmpUsedColor), sizeof(bmpinfophead.bmpUsedColor));
inFile.read((char*)(&bmpinfophead.bmpImportantColor),sizeof(bmpinfophead.bmpImportantColor));
inFile.close();
}
/*void writeRGBQUAD(string &infilename)
{
static rgbDate rgbQUAD[2];
ifstream inFile(infilename.c_str(),ios::beg);
inFile.seekg(54,ios::beg);
for( int i=0;i <2;++i)
{
inFile.read((char*)(&rgbQUAD[i].rgbBlue), sizeof(rgbQUAD[i].rgbBlue));
inFile.read((char*)(&rgbQUAD[i].rgbGreen), sizeof(rgbQUAD[i].rgbGreen));
inFile.read((char*)(&rgbQUAD[i].rgbRed), sizeof(rgbQUAD[i].rgbRed));
inFile.read((char*)(&rgbQUAD[i].rgbReserved), sizeof(rgbQUAD[i].rgbReserved));
}
inFile.close();
}*/
void writeIMGdate (string &infilename, unsigned char uldate[][4])
{
ifstream inFile(infilename.c_str(),ios::binary);
//ofstream outFile(outfilename.c_str(),ios::binary);
inFile.seekg(54,ios::beg);
//char BUF[5];
int i=0;
while(!inFile.eof())
{
inFile.read((char*)(uldate[i]),3); ++i;
}
cout < <i < <endl;cout < < "hao: " < <endl;
inFile.close();
//outFile.close();
}
//bbb.cpp
//bmp_read 2007,04,04,24:38
#include "aaa.cpp "
#include <fstream>
#include <string.h>
#include <iostream>
unsigned char ulDate[400][4];
bmpFILEheader bmpfilehead;
bmpINFOheader bmpinfophead;
int main()
{
string infilename= "blu.bmp ";
string outfilename= "c.txt ";
writebmpFILEhead(infilename, bmpfilehead);
writebmpINFOhead(infilename, bmpinfophead);
//writeRGBQUAD(infilename);
writeIMGdate(infilename,ulDate );
cout < < bmpfilehead.bmpFileSize < <endl;
cout < < bmpinfophead.bmpWidth < <endl;
cout < <endl;
for(int i=0;i <=150;++i)
{
cout < <i < < "行 ";
for(int j=0;j <3;++j) cout < <(unsigned short)(ulDate[i][j]) < < ' ';
cout < <endl;
}
cout < <endl;
}
如果我把aaa.cpp的东西全部剪切到bbb.cpp中则运行成功!
否则不剪切aaa.cpp到bbb.cpp中则无法连接!
下面是输出的错误联接问题:
bbb.obj : error LNK2005: "void __cdecl writebmpFILEhead(class std::basic_string <char,struct std::char_traits <char> ,class std::allocator <char> > &,struct mytagBITMAPFILEHEADER &) " (?writebmpFILEhead@@YAXAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AAUmytagBITMAPFILEHEADER@@@Z) already defined in aaa.obj
[解决办法]
重新安排一下aaa.h头文件,
把aaa.cpp中定义的公用函数放到aaa.h头文件中声明。
//aaa.h
#ifndef _strct_H
#define _strct_H
typedef unsigned char myBYTE;
typedef unsigned short myWORD;
typedef unsigned long myDWORD;
//------------------------------------------------------
typedef struct mytagBITMAPFILEHEADER
{
myWORD bmpFileType;
myDWORD bmpFileSize;
myWORD bmpFileReserved1, bmpFileReserved2;
myDWORD bmpHeadtoImgDateOffset;
} bmpFILEheader, *P_bmpFILEheader;
//----------------------------------------------------------
typedef struct mytagBITMAPINFOHEADER2
{
myDWORD bmpINFOstrctSize;
long bmpWidth, bmpHeight;
myWORD bmpPlanes;
myWORD bmpColorNumDef;
myDWORD bmpCompression;
myDWORD bmpImgSize;
long bmpXPelsPerMeter, bmpYPelsPerMeter;//曋辨率
myDWORD bmpUsedColor, bmpImportantColor;
}bmpINFOheader, *P_bmpINFOheader;
//-----------------------------------------------------------
typedef struct mytagRGBQUAD
{
myBYTE rgbBlue, rgbGreen, rgbRed;
myBYTE rgbReserved;
}rgbDate;
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
void writebmpFILEhead(string &infilename, bmpFILEheader & bmpfilehead);
void writebmpINFOhead(string &infilename, bmpINFOheader & bmpinfophead);
void writeIMGdate (string &infilename, unsigned char uldate[][4]);
#endif;
然后在bbb.cpp文件中include "aaa.h "
//bbb.cpp
//bmp_read 2007,04,04,24:38
#include "aaa.h "