为什么总是提示local function definitions are illegal

t261705606 2011-07-01 04:06:32
int CountLines(char *filename)//获取文件的行数
{
ifstream ReadFile;
int n=0;
string temp;
ReadFile.open(filename,ios::in);//ios::in 表示以只读的方式读取文件
if(ReadFile.fail())//文件打开失败:返回0
{
return 0;
}
else//文件存在,返回文件行数
{
while(getline(ReadFile,temp))
{
n++;
}
return n;
}
ReadFile.close();
}


ifstream file;
int LINES;
char filename[512];
cout<<"请输入要打开的文件名:"<<endl;
cin>>filename;
file.open(filename,ios::in);
if(file.fail())
{
cout<<"文件不存在."<<endl;
file.close();
cin.get();
cin.get();
}
else//文件存在
{
LINES=CountLines(filename);
char *tc=new char[LINES];
char *t=new char[LINES];
int *tcc=new int[LINES];
int x=0;
while(!file.eof()) //读取数据到数组
{
file>>tc[x];
file>>t[x];
file>>tcc[x];
x++;
}
file.close(); //关闭文件
for(x=0;x<LINES;x++)//输出数组内容
cout<<tc[x]<<"\t"<<t[x]<<"\t"<<tcc[x]<<endl;
cin.get();
cin.get();
}




总是提示error C2601: 'CountLines' : local function definitions are illegal有没有高手帮我看看啊
谢谢
...全文
548 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
iambic 2011-07-01
  • 打赏
  • 举报
回复
这种问题只要缩进清晰就是一目了然的吧。让你不格式化代码。
就想叫yoko 2011-07-01
  • 打赏
  • 举报
回复


int main()
{
void fun()
{

}

return 0;
}
这样是不允许的 需要改成下面这样


void fun()
{

}

int main()
{
fun();//这里调用不调用看你自己需求
return 0;
}
t261705606 2011-07-01
  • 打赏
  • 举报
回复
怎么提出来
[Quote=引用 3 楼 q191201771 的回复:]
CountLine这个函数的定义提出来~~
[/Quote]
t261705606 2011-07-01
  • 打赏
  • 举报
回复
要是把我发的那段代码作为一个函数用怎么改
c_losed 2011-07-01
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>
#include <Windows.h>
#include <string>
using namespace std;

int CountLines(char *filename)//获取文件的行数
{
ifstream ReadFile;
int n=0;
string temp;
ReadFile.open(filename,ios::in);//ios::in 表示以只读的方式读取文件
if(ReadFile.fail())//文件打开失败:返回0
{
return 0;
}
else//文件存在,返回文件行数
{
while(getline(ReadFile,temp))
{
n++;
}
return n;
}
ReadFile.close();
}




int main()
{
ifstream file;
int LINES;
char filename[512];
cout<<"请输入要打开的文件名:"<<endl;
cin>>filename;
file.open(filename,ios::in);
if(file.fail())
{
cout<<"文件不存在."<<endl;
file.close();
cin.get();
cin.get();
}
else//文件存在
{
LINES=CountLines(filename);
char *tc=new char[LINES];
char *t=new char[LINES];
int *tcc=new int[LINES];
int x=0;
while(!file.eof()) //读取数据到数组
{
file>>tc[x];
file>>t[x];
file>>tcc[x];
x++;
}
file.close(); //关闭文件
for(x=0;x<LINES;x++)//输出数组内容
cout<<tc[x]<<"\t"<<t[x]<<"\t"<<tcc[x]<<endl;
cin.get();
cin.get();
}

}

只保证了编译通过
t261705606 2011-07-01
  • 打赏
  • 举报
回复
ifstream file一下是另一个函数,里面有对CountLines的调用
luciferisnotsatan 2011-07-01
  • 打赏
  • 举报
回复
int CountLines(char *filename)//获取文件的行数
{
ifstream ReadFile;
int n=0;
string temp;
ReadFile.open(filename,ios::in);//ios::in 表示以只读的方式读取文件
if(ReadFile.fail())//文件打开失败:返回0
{
return 0;
}
else//文件存在,返回文件行数
{
while(getline(ReadFile,temp))
{
n++;
}
return n;
}
ReadFile.close();
} // 配对最上边的{

// 把函数定义在函数里了??
ifstream file;
int LINES;
char filename[512];
cout<<"请输入要打开的文件名:"<<endl;
就想叫yoko 2011-07-01
  • 打赏
  • 举报
回复
CountLine这个函数的定义提出来~~
bdmh 2011-07-01
  • 打赏
  • 举报
回复
从ifstream file; 这行开始的代码属于谁啊,都出了'CountLines' 的代码段了,仔细看看吧
luciferisnotsatan 2011-07-01
  • 打赏
  • 举报
回复
Error Message
'function' : local function definitions are illegal


Code tries to define a function within a function.

Or, there may be an extra brace in your source code before the location of the C2601 error.

The following sample generates C2601:

// C2601.cpp
int main() {
int i = 0;

void funcname(int j) { // C2601
j++;
}
}

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧