所以我是编程的新手,我正在学习一个教程,我得到了fstream,但是我不知道我的编译器是不是很奇怪,或者我错过了一个文件或其他东西,但.open函数呢似乎不起作用,而fstream表现得很奇怪。 (就像你不能使用(ostreamobject)(“test.txt”);我是编程的新手,所以请不要使用技术术语。
我搜索了一下但我没找到任何东西。
我不知道我的代码或编译器有什么问题但是outputFile.open并不存在。我正在使用visual studio 2015.这是我编写的少量代码,但仍然存在错误。这是代码:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ostream oFile;
istream iFile;
oFile.open("test.txt");
return 0;
}
这是错误代码:
1&gt; d:\ dokument \ visual studio 2015 \ projects \ fstream \ fstream \ fstream.cpp(13):错误C2512:'std :: basic_ostream&gt;':没有合适的默认构造函数可用 1 GT; d:\ programmering \ _vc \ include \ iosfwd(679):注意:请参阅'std :: basic_ostream&gt;'的声明 1&gt; d:\ dokument \ visual studio 2015 \ projects \ fstream \ fstream \ fstream.cpp(14):错误C2512:'std :: basic_istream&gt;':没有合适的默认构造函数可用 1 GT; d:\ programmering \ vc \ include \ iosfwd(678):注意:请参阅'std :: basic_istream&gt;'的声明 1&gt; d:\ dokument \ visual studio 2015 \ projects \ fstream \ fstream \ fstream.cpp(16):错误C2039:'open':不是'std :: basic_ostream&gt;'的成员 1 GT; d:\ programmering \ _vc \ include \ iosfwd(679):注意:请参阅'std :: basic_ostream&gt;'的声明 ==========构建:0成功,1个失败,0个最新,0个跳过==========
答案 0 :(得分:2)
问题是,你正在使用&#34; ostream&#34;和#34; istream&#34;而不是&#34; o f stream&#34;和&#34;我 f 流&#34; (注意&#34; f &#34;&#34; f ile&#34;)。
使用此版本:
#include "stdafx.h"
// #include <iostream> // you don't need this and it caused most of your confusion!
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream oFile;
ifstream iFile;
oFile.open("test.txt");
return 0;
}
供参考: &#34; ofstream的&#34;和&#34; ifstream&#34;都是&#34; ostream&#34;的超类。和#34; istream&#34;。它们提供了与文件交互的更多功能(例如&#34; open&#34;)
答案 1 :(得分:1)
嗯,不存在。没有cdist
构造函数采用文件名。
您的意思是ostream
。
您可以通过访问文档来检查这一点。
如果您的教程真的说ofstream
,请告诉我们它是什么并停止使用它
你应该从a good book学习C ++,而不是从互联网上的随机“tuts”学习。
答案 2 :(得分:0)
考虑将oFile和iFile声明为具体文件。
ofstream oFile;
ifstream iFile;