首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++模板参数失败,不是从错误派生的。

问C++模板参数失败,不是从错误派生的。
EN

Stack Overflow用户
提问于 2015-12-13 19:53:28
回答 2查看 1.3K关注 0票数 0

我试图从一个文件中读取,但我得到了这个错误。

/usr/include/c++/4.8/bits/basic_string.tcc:1068:5:注释:模板参数演绎/替换失败: prog.cc:35:38:注意:‘std::ofstream {aka std::basic_ofstream}’不是从‘std::basic_istream<_CharT,_Traits>’派生的

代码语言:javascript
复制
   while(getline(input_file, line_)){
                                  ^

我对C++比较陌生,所以我不知道这个错误到底意味着什么。任何帮助都将不胜感激,谢谢。这是我的密码。

代码语言:javascript
复制
#include "prog.hh"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool prog::readFile() {

ofstream input_file("myText.txt");
string line_; // string which text file contents will be stored in

if(input_file.is_open()){ // validation to see if the file is open
  while(getline(input_file, line_)){
      cout<<line_<< '\n'; //prints the contents of the file into the console
  }
  input_file.close();
}
else {
  cout<<"File is not open"<< '\n';
}
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-13 20:02:14

std::ofstream (out文件流)用于写入文件。您不能从std::ofstream中读取。使用std::ifstream (in文件流)读取文件,如果需要使用相同的流来读取和写入文件,则使用std::fstream。

票数 0
EN

Stack Overflow用户

发布于 2015-12-13 20:05:58

ofstream用于输出,您需要ifstream。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34255595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档