开发者社区> 问答> 正文

c++文件读写报错class“std::basic_ifstream?报错

用c++写了段代码,读文件的,但是报错,在网上搜不到问题,哪位以c++高手帮忙看下,谢谢

报错信息:

 error C2079: “file”使用未定义的 class“std::basic_ifstream<_Elem,_Traits>”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]

以下是代码:环境是vs2012

#include <iostream>
#include <ios>
#include <ostream>
#include <iomanip>
#include <streambuf>
#include <string>
#include <iosfwd>
#include <fstream>
#include <istream>
using namespace std;
class StuService{
public: void Input();
		int Total();
		double Average();

};

void StuService::Input(){
	char buffer[256];
	ifstream file;
	file.open("d://records.txt");
	
	if(!file){
		cout<<"打开records.txt文档异常";
		exit(0);
	}

	int a,b;
	int i=0,j=0;
	
	while(!file.eof()){
		file.getline(buffer,10);
		sscanf(buffer,"%d %d",&a,&b);
		cout<<a<<";"<<b<<endl;
	}
	file.close();
};
int StuService :: Total(){
	return 0;
};
double StuService::Average(){
	return  0.0;
}
void main(){
	StuService service ;
	service.Input();

}



展开
收起
爱吃鱼的程序员 2020-06-22 13:50:12 864 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    #include<iosfwd>

    把这行去掉试试吧

    像是模板没有实例化,查下 iosfwd 中 ifstream的定义,手头没2012,没法看。

    你没说清楚是哪一行报的。。。

    令人巨汗,vs2010下边能编译能运行

    虽然不知道题主想干什么…但是C和C++混合风格的写法是在太丑了…

    #include<iostream>#include<fstream>#include<string>#include<sstream>


    ...

        inta,b;strings;stringstreamss;while(getline(file,s)){ss<<s;ss>>a>>b;ss.clear();cout<<a<<";"<<b<<endl;}我是初学者,
    2020-06-22 13:50:30
    赞同 展开评论 打赏
问答分类:
C++
问答地址:
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载