第二次运行C++时无法编译
Fail to compile second time I run C++
我正在尝试保存一些数据以供以后在.txt文件中使用。我第一次运行代码,没问题。第二次,所有的地狱都松动了。我写到文件中的函数如下:
void VideoSelection::write(char Name[255], char address[255])
{
int i = 0;
string iString;
saveFile.open("Movies.txt");
for (string line; getline(input, line); ) // check for the number of movies (0-index)
{
iString = to_string(i);
if (line == iString)
{
i++;
}
}
saveFile << i << endl;
saveFile << "NAME: " << Name << " " << "ADDRESS: " << address << endl << endl;
saveFile.close();
}
使用的头文件如下:
#pragma once
#include <iostream>
#include <fstream>
#include <sstream>
#include "Movies.txt";
using namespace std;
class VideoSelection
{
public:
VideoSelection();
void write(char Name[255], char address[255]);
void read();
void sort();
void open();
~VideoSelection();
protected:
char http[255];
ofstream saveFile;
ifstream input;
};
和主要:
#include "VideoSelection.h"
int main()
{
VideoSelection VS;
char movieName[255];
char movieAddress[255];
cin >> movieName;
cin >> movieAddress;
VS.write(movieName, movieAddress);
}
这些是我遇到的错误
https://gyazo.com/f5f13e7dfd18378152df6126b7a40be1
始终从第一个错误开始。(第 6 行上意外的预处理器令牌(
#include "Movies.txt";
删除分号,使其
#include "Movies.txt"
但是等等 - 为什么要包含非代码 TEXT 文件作为预处理器指令? 这可能就是第二条错误消息的内容。 :)
我怀疑你只需要完全删除那条线。
另一个旁注:
using namespace std;
尽量不要在头文件中执行using namespace
指令。 仅在.cpp文件中。在该类中正式声明流成员,如下所示:
std::ofstream saveFile;
std::ifstream input;
您正在使用文本文件作为预处理器指令。编译器正在查看.txt文件并像分析代码文件一样对其进行分析,并对它看起来像语法错误的内容大喊大叫。
相关文章:
- 如何在linux终端中同时编译和运行c++代码
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 尝试用java代码编译和运行c++代码
- 热键/按钮,根据需要进行编译,但不运行(在F5和Ctrl+Shift+B之间)
- 编译并运行后,输出未出现在 Visual Studio 代码中
- 在运行时解析函数,而不是在编译C++解析函数
- 编译多个C++文件.调用二进制文件以运行代码
- 在编译时,C++项目抛出错误 C2228,这是预期的,因为控件在运行时未达到该点
- 是否可以编译 Gtk+ 应用程序以同时在 Gtk 2 和 3 上运行
- 在编译时为运行时设置环境变量
- 如何让谷歌测试正常运行。测试总是失败。(它不会编译)
- 运行时检查失败 #0 用于运行时重新编译
- 如何从测试脚本中运行WAF编译C++程序?
- std::tuple 可以根据其值在编译时/运行时排序吗?
- 在iPhone上编译/运行C++代码
- 在编译/运行时将字符串文本的原始字节流入/流出Windows(非宽)执行字符集,以及ANSI代码页与UTF-8
- Visual Studio、CodeBlocks和Eclipse中的C++编译/运行问题
- c++相同的代码永远不会在Visual Studio中编译/运行,有时在Qt Creator中编译/运行
- 是否可以基于宏或变量有条件地编译/运行代码