Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 文件";“未在范围内声明”;即使是_C++_File_Scope - Fatal编程技术网

C++ 文件";“未在范围内声明”;即使是

C++ 文件";“未在范围内声明”;即使是,c++,file,scope,C++,File,Scope,我的程序的一部分包括写入文本文件。我有: #include<fstream> #include<iostream> #include<string> #include<cstdlib> #include<ctime> using namespace std; //I open the file "highscore.txt" here, read it, and then close it. ofstream outfile;

我的程序的一部分包括写入文本文件。我有:

#include<fstream>
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>

using namespace std;

//I open the file "highscore.txt" here, read it, and then close it.

ofstream outfile;

outfile.open("highscore.txt", ios::out);

outfile<<score;

outfile.close();
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//我在这里打开文件“highscore.txt”,读取它,然后关闭它。
出流孔的直径;
open(“highscore.txt”,ios::out);
输出文件
编辑:因为根据OP,代码在一个函数中,并且定义了
分数
,这很可能是计算机的情况 我是个混蛋。从理论上讲,重启应该可以解决这个问题

代码的逻辑是正确的。从语法上讲,有两个主要问题:它必须位于函数中,并且必须定义分数(尽管这与此错误无关)

我猜您来自Python这样的语言,可以直接运行代码。正如Nathan Oliver和Fabio Turati指出的,你似乎缺少一个<代码> int()/<代码>函数,这是所有C和C++程序都需要的。
请查看for of Stream以查看代码的工作版本。

我只看到全局范围。你想干什么?你有MCVE吗?你的
main()
函数在哪里?你的主要函数在哪里?您不能在全局范围内运行代码这不是一个完整的示例,请提供一个更有意义的示例。@skypjack我怀疑OP的问题在于这是他的完整示例:)嗯,没有定义和
分数。是的,也是这样。错过了这一点。>>它根本不正确,除非名为
score
的变量在代码中神奇地出现…:-)@skypjack这只是我程序的一小部分。我定义了分数,当然我有主要功能above@Ovi:您需要发布一个示例,实际显示编译时出现的错误。我把你的代码放在一个
main()
中,并添加了
score
,它编译并运行了。看看