How to resolve "error: ‘The’ does not name a type" when reading from txt file?

How to resolve "error: ‘The’ does not name a type" when reading from txt file?

目前正在尝试使用我创建的 C++ 从文本文件中读取并让它循环显示单词。我尝试使用 fstream 和 istream 但出于某种原因我仍然收到此错误消息

HarlemRenaissance.txt:1:1: error: ‘The’ does not name a type
    1 | The Harlem Renaissance was an intellectual 

有人知道问题出在哪里吗?

这是我的代码片段:

#include <iostream>
#include <fstream>
#include <stdio.h>
#include "HarlemRenaissance.txt"
using namespace std;

int main() {
    //reads file
    // Create a text string, which is used to output the text file
    string myText;

    // Read from the text file
    std::ifstream MyReadFile("HarlemRenaissance.txt");

    // Use a while loop together with the getline() function to read the file line by line
    while (std::getline (MyReadFile,myText)) {
        
        // Output the text from the file
        std::cout << myText << std::endl;

    }
    
    //output data 
    outputStats();
    
    //closes file 
    MyReadFile.close();
}

从您的代码中删除 #include "HarlemRenaissance.txt"。实际上包括意味着复制粘贴。包含文件的内容将粘贴到源文件中。在这种情况下,您的文件内容将粘贴到您的源代码中。也许它有一些语法不正确的词。这就是您收到此错误消息的原因。