简体   繁体   中英

Fail to compile second time I run C++

I a'm trying to to save some data for later use in a .txt file. The first time i run the code, no problem. The second time, all hell breaks loose. The function I you to write into the file is as following:

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();
}

and the header file used is as following:

#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;
};

and the main:

#include "VideoSelection.h"

int main()
{
    VideoSelection VS;
    char movieName[255];
    char movieAddress[255];
    cin >> movieName;
    cin >> movieAddress;

    VS.write(movieName, movieAddress);
}

These are the errors I'm getting

https://gyazo.com/f5f13e7dfd18378152df6126b7a40be1

Always start with the first error. (unexpected preprocessor token on line 6)

#include "Movies.txt";

Remove the semicolon such that it's

#include "Movies.txt"

But wait - why are you including a non-code TEXT file as a pre-processor directive? That's probably what the second error message is about. :)

I suspect you just need to remove that line completely.

Another side note:

using namespace std;

Try not to do a using namespace directive in a header file. Only in the .cpp files. formally declare your stream members in that class as follows:

std::ofstream saveFile;
std::ifstream input;

You are using a text file as pre-processor directive. The compiler is looking at the .txt file and analyze it as if it was a code file, and scream at what it looks as syntax errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM