读书人

两个函数其间的数组调用以及函数调用

发布时间: 2013-01-28 11:49:56 作者: rapoo

两个函数之间的数组调用以及函数调用
void CWorkView::Onsave()
{
// TODO: Add your control notification handler code here
extern str;
extern i;
CString pstn[1000];
int j=0;
pstn[j]=str[i];
j++;
m_edit=" ";

}
void CWorkView::Oncatch()
{
// TODO: Add your control notification handler code here
CString str[1000];
CString temp;
int i=0;
char*pszFileName="C:\\a.txt";
CStdioFile a;
CFileException fileException;
if(a.Open(pszFileName,CFile::modeReadWrite),&fileException)
{
a.SeekToBegin();
do{
a.ReadString(str[i]);
temp.Format("%3d",str[i]);
m_edit=temp;
CWorkView::Onsave();
UpdateData(false);
i++;
}while(str[i-1]!="%");
//读出a文件中的信息,并显示;
}
else
{
TRACE("Can't open file %s,error=%u\n",pszFileName,fileException.m_cause);
}
a.Close();
}

错误代码:error C2109: subscript requires array or pointer type
执行 cl.exe 时出错.

onsave()函数调用oncatch()的str[]数组时出错!!
请高手赐教~~~~
[解决办法]
我宁愿用类的静态变量来解决,也不愿意看到extern这处关键字

你试试把与str[]相关的,转换成类静态变量试试
CString str[10]
换成在类声明里的 static CString str[10]

[解决办法]
::Onsave(CString str[],...)
[解决办法]
楼上的解决办法很好,不过少了变量i。应该是这样:


void CWorkView::Onsave(int i, CString* str)
{
// TODO: Add your control notification handler code here
// extern这两句删除不要

调用的时候

Onsave(i, str);

[解决办法]
看了你问题里的代码,发现问题很多。
int j = 0;
pstn[j]=str[i];
j++;
每次执行到这里,j的值都是0,然后j++变成1,并不能实现你想把数组的值保存在pstn数组的目的。

猜想你这段代码的目的,做了以下的修改:
// CWorkView定义
class CWorkView
{
//以下是增加的部分
private:
int m_Count;//初始化时要归零,在CWorkView()中
CString m_str[1000];
CString m_pstn[1000];
};


// CWorkView实现
void CWorkView::Onsave(int Index)
{
pstn[m_Count++]=m_str[Index];
m_edit=" ";
}

void CWorkView::Oncatch()
{
// TODO: Add your control notification handler code here
CString temp;//这里temp是做什么用的?
int i=0;
char* pszFileName="C:\\a.txt";
CStdioFile a;
CFileException fileException;
if(a.Open(pszFileName, CFile::modeReadWrite), &fileException)
{
a.SeekToBegin();
do
{
a.ReadString(m_str[i]);
temp.Format("%3d", m_str[i]);
m_edit = temp;
Onsave(i++);
UpdateData(false);
} while(m_str[i-1] != "%");
}
else
{
TRACE(_T("Can't open file %s,error=%u\n"), pszFileName, fileException.m_cause);


}
a.Close();
}


[解决办法]
error C2511: 'Onsave' : overloaded member function 'void (int,class CString *)' not found in 'CWorkView'
see declaration of 'CWorkView'
error C2660: 'Onsave' : function does not take 2 parameters

你在类定义中没有修改函数原型啊?
void Onsave();
要修改为
void Onsave(int i, CString str);
[解决办法]
类定义时,数组只开了1000
看看是不是数据多过1000条导致的内存溢出?

读书人网 >VC/MFC

热点推荐