全局变量:
CDC MemDC[30];
CBitmap bm[30];BOOL CTestcaptureDlg::SaveBufToFile(LPCTSTR filename, CBitmap bm, CDC dc)
{
BITMAP btm;
bm.GetBitmap(&btm);
DWORD size=btm.bmWidthBytes*btm.bmHeight;
LPSTR lpData=(LPSTR)GlobalAlloc(GPTR,size);
/////////////////////////////////////////////
BITMAPINFOHEADER bih;
bih.biBitCount=btm.bmBitsPixel;
bih.biClrImportant=0;
bih.biClrUsed=0;
bih.biCompression=0;
bih.biHeight=btm.bmHeight;
bih.biPlanes=1;
bih.biSize=sizeof(BITMAPINFOHEADER);
bih.biSizeImage=size;
bih.biWidth=btm.bmWidth;
bih.biXPelsPerMeter=0;
bih.biYPelsPerMeter=0;
GetDIBits(dc,bm,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
static int filecount=0;
CString name;
name.Format("pict%04d.bmp",filecount++);
BITMAPFILEHEADER bfh;
bfh.bfReserved1=bfh.bfReserved2=0;
bfh.bfType=((WORD)('M'<< 8)|'B');
bfh.bfSize=54+size;
bfh.bfOffBits=54;
CFile bf;
if(bf.Open(name,CFile::modeCreate|CFile::modeWrite))
{
bf.WriteHuge(&bfh,sizeof(BITMAPFILEHEADER));
bf.WriteHuge(&bih,sizeof(BITMAPINFOHEADER));
bf.WriteHuge(lpData,size);
bf.Close();
}
GlobalFree(lpData); return TRUE;
}void CTestcaptureDlg::OnButton2() 
{
CDC dc;
CString strtmp;
for (int i = 0; i < 30; i++)
{
strtmp.Format("tmp_%d.bmp", i);
SaveBufToFile(strtmp, bm[i], dc);
}
}问题:编译时报错:error C2664: 'SaveBufToFile' : cannot convert parameter 2 from 'class CBitmap' to 'class CBitmap'
为什么?

解决方案 »

  1.   

    void CTestcaptureDlg::OnButton2() 
    {
    CDC dc;
    CString strtmp;
    for (int i = 0; i < 30; i++)
    {
    strtmp.Format("tmp_%d.bmp", i);出错行————》  SaveBufToFile(strtmp, bm[i], dc);
    }
    }
      

  2.   

    某参数无法转换为所需类型。如果创建某个类的实例,然后试图对用 explicit 关键字标记的构造函数进行隐式转换,则可能发生此错误。如果将临时对象传递给采用指向对象的引用作为参数的函数,则该引用必须是常数引用。如果使用不是函数所预期的类型的参数调用该函数,则使用适当的构造函数创建临时对象。然后将该临时对象传递给函数。在这种情况下,该临时对象用于初始化引用。在该语言的早期版本中,所有的引用都可以由临时对象进行初始化。此行为现在已被逐步淘汰,因此 Microsoft C/C++ 编译器给出该错误还是用引用类型
    BOOL CTestcaptureDlg::SaveBufToFile(LPCTSTR filename, CBitmap& bm, CDC& dc)
      

  3.   

    你的SaveBufToFile用的是传值的方法,那么bm[i],dc这两个参数会调用CBitmap 和CDC的拷贝
    构造函数,而MFC中没有提供这两个函数,所以无法通过编译的。
    写成这样就可以了,
    BOOL SaveBufToFile(LPCSTR,CBitmap*,CDC*);
    然后在调用SaveBufToFile(strtmp, &bm[i], &dc);另外提醒你一下,你对编译错误提示看
    的不是很仔细,其实C2440写的很详细了。(no copy construtor avaliable)