第二次运行C++时无法编译

Fail to compile second time I run C++

本文关键字:编译 运行 C++ 第二次      更新时间:2023-10-16

我正在尝试保存一些数据以供以后在.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文件并像分析代码文件一样对其进行分析,并对它看起来像语法错误的内容大喊大叫。