C++:尝试传递 fstream 作为参数时删除了函数?

发布于 2025-01-10 20:03:48 字数 2202 浏览 2 评论 0原文

我不知道我的代码有什么问题。我试图从控制台获取两个文件的文件路径,然后用这些文件和 ios::in | 初始化一些 fstream 对象。一个是 ios::out,另一个是添加 ios::binary

以下是我的代码的重要部分:

// Function prototypes
void INPUT_DATA(fstream);
void INPUT_TARGETS(fstream);

int main()
{
    // Ask the user to specify file paths
    string dataFilePath;
    string targetsFilePath;
    cout << "Please enter the file paths for the storage files:" << endl
        << "Data File: "; 
    getline(cin, dataFilePath); // Uses getline() to allow file paths with spaces
    cout << "Targets File: "; 
    getline(cin, targetsFilePath);

    // Open the data file
    fstream dataFile;
    dataFile.open(dataFilePath, ios::in | ios::out | ios::binary);

    // Open the targets file
    fstream targetsFile;
    targetsFile.open(targetsFilePath, ios::in | ios::out);

    // Input division data into a binary file, passing the proper fstream object        
    INPUT_DATA(dataFile);

    // Input search targets into a text file
    INPUT_TARGETS(targetsFile);

    ...
}

// Reads division names, quarters, and corresponding sales data, and writes them to a binary file
void INPUT_DATA(fstream dataFile)
{
    cout << "Enter division name: ";
    ... 
    dataFile << divisionName << endl;
    ...
}

// Reads division names and quarters to search for, and writes them to a file
void INPUT_TARGETS(fstream targetsFile)
{
    cout << "\nPlease input the search targets (or \"exit\"):";
    ...
    targetsFile.write( ... );
    ...
}

但是,Visual Studio 在 INPUT_DATA(dataFile);INPUT_TARGETS(targetsFile); 部分对我大喊大叫,说:

function "std::basic_fstream<_Elem, _Traits>::basic_fstream(const std::basic_fstream<_Elem, _Traits>::_Myt &) [with _Elem=char, _Traits=std::char_traits<char>]" (declared at line 1244 of "c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\fstream") cannot be referenced -- it is a deleted function

我四处挖掘在头文件中,直到我找到第 1244 行:

basic_fstream(const _Myt&) = delete;

我不知道为什么会发生这种情况。我对 C++ 还很陌生,我可能刚刚做了一些愚蠢的事情,但是有人可以帮忙吗?

编辑:澄清标题

I don't know what is wrong with my code. I am trying to get a file path from the console for two files, then I initialize some fstream objects with those files and ios::in | ios::out for one, and an addition of ios::binary for the other.

Here are the important parts of my code:

// Function prototypes
void INPUT_DATA(fstream);
void INPUT_TARGETS(fstream);

int main()
{
    // Ask the user to specify file paths
    string dataFilePath;
    string targetsFilePath;
    cout << "Please enter the file paths for the storage files:" << endl
        << "Data File: "; 
    getline(cin, dataFilePath); // Uses getline() to allow file paths with spaces
    cout << "Targets File: "; 
    getline(cin, targetsFilePath);

    // Open the data file
    fstream dataFile;
    dataFile.open(dataFilePath, ios::in | ios::out | ios::binary);

    // Open the targets file
    fstream targetsFile;
    targetsFile.open(targetsFilePath, ios::in | ios::out);

    // Input division data into a binary file, passing the proper fstream object        
    INPUT_DATA(dataFile);

    // Input search targets into a text file
    INPUT_TARGETS(targetsFile);

    ...
}

// Reads division names, quarters, and corresponding sales data, and writes them to a binary file
void INPUT_DATA(fstream dataFile)
{
    cout << "Enter division name: ";
    ... 
    dataFile << divisionName << endl;
    ...
}

// Reads division names and quarters to search for, and writes them to a file
void INPUT_TARGETS(fstream targetsFile)
{
    cout << "\nPlease input the search targets (or \"exit\"):";
    ...
    targetsFile.write( ... );
    ...
}

However, Visual Studio yells at me on the INPUT_DATA(dataFile); and INPUT_TARGETS(targetsFile); parts, saying:

function "std::basic_fstream<_Elem, _Traits>::basic_fstream(const std::basic_fstream<_Elem, _Traits>::_Myt &) [with _Elem=char, _Traits=std::char_traits<char>]" (declared at line 1244 of "c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\fstream") cannot be referenced -- it is a deleted function

I dug around in the header file until I found line 1244:

basic_fstream(const _Myt&) = delete;

I have no idea why this is happening. I'm still fairly new to C++ and I've probably just done something idiotic, but could someone please help?

EDIT: Clarified title

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦爱 2025-01-17 20:03:48

您无法复制 std::fstream,因此复制构造函数被删除,正如您通过深入研究发现的那样:)

也没有理由复制 std::fstream< /代码>。在您的情况下,您希望通过引用传递它,因为您想要修改原始的 std::fstream (您在 main 中创建的),而不是创建整个新的(这就是删除复制构造函数的原因,顺便说一句 :))。

You can't copy a std::fstream, so the copy constructor is deleted, as you found out by digging around :)

There is also no reason to copy a std::fstream. In your case, you want to pass it by reference, because you want to modify the original std::fstream, the one you created in main, and not create a whole new one (that's why the copy constructor is deleted, btw :)).

迷爱 2025-01-17 20:03:48

这是因为 std::fstream 的复制构造函数被删除了。您不能按值传递它。
要解决此问题,请通过引用传递 std::fstream,如下所示:

void INPUT_DATA(fstream& dataFile) { /* ... */ }
void INPUT_TARGETS(fstream& targetsFile) { /* ... */ }

您不必更改代码中的任何其他内容。

That's because the copy-constructor of std::fstream's is deleted. You cannot pass it by value.
To solve this, pass the std::fstream's by reference, like so:

void INPUT_DATA(fstream& dataFile) { /* ... */ }
void INPUT_TARGETS(fstream& targetsFile) { /* ... */ }

You don't have to change anything else in the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文