Visual Studio 2010 CFileDialog错误 [英] Visual Studio 2010 CFileDialog error

查看:114
本文介绍了Visual Studio 2010 CFileDialog错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!我的以下代码有问题,它给我CFileDialog部分一个错误:

错误C2664:``CFileDialog :: CFileDialog(BOOL,LPCTSTR,LPCTSTR,DWORD,LPCTSTR,CWnd *,DWORD,BOOL)'':无法将参数2从``const char [5]''转换为``LPCTSTR''

Hello! I have a problem with the following code, it gives me an error to CFileDialog part:

error C2664: ''CFileDialog::CFileDialog(BOOL,LPCTSTR,LPCTSTR,DWORD,LPCTSTR,CWnd *,DWORD,BOOL)'' : cannot convert parameter 2 from ''const char [5]'' to ''LPCTSTR''

void CROSH1Dlg::OnBnClickedSave()
{
	UpdateData();
	CStudentGrade StdGrades;
	if( this->m_StudentName == "" )
		return;
	//strcpy (StdGrades.StudentName, this->m_StudentName);
	StdGrades.StudentName = this->m_StudentName;
	StdGrades.SchoolYear1 = this->m_SchoolYear1;
	StdGrades.SchoolYear2 = this->m_SchoolYear2;
	StdGrades.English     = this->m_English;
	StdGrades.History     = this->m_History;
	StdGrades.Economics   = this->m_Economics;
	StdGrades.Language2   = this->m_2ndLanguage;
	StdGrades.Geography   = this->m_Geography;
	StdGrades.Arts        = this->m_Arts;
	StdGrades.Math        = this->m_Math;
	StdGrades.Science     = this->m_Science;
	StdGrades.PhysEduc    = this->m_PhysEduc;
	StdGrades.Total       = this->m_Total;
	StdGrades.Average     = this->m_Average;

	CString strFilename = this->m_StudentName;
	CFile fleGrades;
	char strFilter[] = { "Student Grades (*.dnt)|*dnt|All Files (*.*)|*.*||" };
	CFileDialog dlgFile(FALSE, ".dnt", strFilename, 0, strFilter);

	if( dlgFile.DoModal() == IDOK )
	{
		// Save the record in binary format
		ofstream stmGrades(dlgFile.GetFileName(), ios::binary);
		stmGrades.write((char *)&StdGrades, sizeof(StdGrades));

		// Reset the dialog box in case the user wants to enter another record
		m_StudentName = "";
		m_SchoolYear1 = 2000;
		m_SchoolYear2 = 2001;
		m_English     = 0.00;
		m_History     = 0.00;
		m_Economics   = 0.00;
		m_2ndLanguage = 0.00;
		m_Geography   = 0.00;
		m_Arts        = 0.00;
		m_Math        = 0.00;
		m_Science     = 0.00;
		m_PhysEduc    = 0.00;
		m_Total       = 0.00;
		m_Average     = 0.00;
	}

	UpdateData(FALSE);
}

推荐答案

使用字符串时,请像这样使用它们
_T("Your String..")
所以要解决您的问题,请使用此代码
When you are using strings use them like this
_T("Your String..")
So to solve your problem use this code
CFileDialog dlgFile(FALSE,_T(".dnt"), strFilename, 0, strFilter);


和使用..
TCHAR strFilter[] = _T("Student Grades (*.dnt)|*dnt|All Files (*.*)|*.*||" );


and use..
TCHAR strFilter[] = _T("Student Grades (*.dnt)|*dnt|All Files (*.*)|*.*||" );


尝试以下操作:
CString strDefExt = ".dnt";
CFileDialog dlgFile(FALSE, strDefExt, strFilename, 0, strFilter);


它正在工作,谢谢您的帮助,但是我还有另一个问题打开按钮,出现两个错误:


错误C2664:``无效的ATL :: CStringT< basetype,stringtraits> :: Format(const wchar_t *,...)'':无法将参数1从``const char [12]''转换为``const wchar_t * ''

It''s working, thank you for your help, however I have another problem with the open button, two errors:


error C2664: ''void ATL::CStringT<basetype,stringtraits>::Format(const wchar_t *,...)'' : cannot convert parameter 1 from ''const char [12]'' to ''const wchar_t *''

caption.Format("Student: %s", name);



错误C2664:``无效的ATL :: CStringT< basetype,stringtraits> :: Format(const wchar_t *,...)'':无法将参数1从``const char [3]''转换为``const wchar_t * ''



error C2664: ''void ATL::CStringT<basetype,stringtraits>::Format(const wchar_t *,...)'' : cannot convert parameter 1 from ''const char [3]'' to ''const wchar_t *''

this->m_StudentName.Format("%s", name);




完整代码在这里,我尝试创建一个打开按钮:




Full code here, I try to make a open button:

void CROSH1Dlg::OnBnClickedOpen()
{
	UpdateData();

	CStudentGrade StdGrades;
	CFile fleGrades;
	
	//TCHAR strFilter[] = _T("Student Grades (*.dnt)|*dnt|All Files (*.*)|*.*||" );
	//CFileDialog dlgFile(FALSE,_T(".dnt"), strFilename, 0, strFilter);
	
	TCHAR strFilter[] = _T("Student Grades (*.dnt)|*dnt|All Files (*.*)|*.*||");
	CFileDialog dlgFile (TRUE,_T(".dnt"), NULL, 0, strFilter);

	if( dlgFile.DoModal() == IDOK )
	{
		// Open a record in binary format
		ifstream stmGrades(dlgFile.GetFileName(), ios::binary);
		stmGrades.read((char *)&StdGrades, sizeof(StdGrades));

		// Change the caption of the dialog box to reflect the current grade
		char name[40];
		//strcpy(name, StdGrades.StudentName);
		 StdGrades.StudentName=name;
		CString caption;
		caption.Format("Student: %s", name);
		this->SetWindowText(caption);

		// Display the record in the dialog box
		this->m_StudentName.Format("%s", name);
		this->m_SchoolYear1 = StdGrades.SchoolYear1;
		this->m_SchoolYear2 = StdGrades.SchoolYear2;
		this->m_English     = StdGrades.English;
		this->m_History     = StdGrades.History;
		this->m_Economics   = StdGrades.Economics;
		this->m_2ndLanguage = StdGrades.Language2;
		this->m_Geography   = StdGrades.Geography;
		this->m_Arts        = StdGrades.Arts;
		this->m_Math        = StdGrades.Math;
		this->m_Science     = StdGrades.Science;
		this->m_PhysEduc    = StdGrades.PhysEduc;
		this->m_Total       = StdGrades.Total;
		this->m_Average     = StdGrades.Average;
	}

	UpdateData(FALSE);
}


这篇关于Visual Studio 2010 CFileDialog错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆