【问题标题】:fstream vector C流向量 C
【发布时间】:2011-03-23 05:26:31
【问题描述】:

我正在尝试使用 vector 和 fstream 从 C 文件中读取和存储该行。我使用的是 Microsoft Visual Studio 2005。问题是当我编译程序时,它说找不到指定的文件如果我使用.h,则包括在内。如果我不使用 .h,那么它会在我将向量和 ifstream 定义为未声明的标识符的正文中显示错误。

谢谢。

【问题讨论】:

  • vectorifstream 不是C 的一部分,它是C++ 的一部分。你确定你的构建环境是正确的吗?
  • 能贴出有问题的代码吗?

标签: c vector ifstream


【解决方案1】:

您不能在 C 中使用 C++ 类 vectorfstream,C 编译器无法编译它们。因此,您要么必须将文件更改为 .cpp(并将其编译为 C++),要么使用 C 语言及其文件处理方法 (fopen, fprint...) 和数组而不是向量。

包括

 #include <stdio.h>

改为&lt;iostream&gt;

【讨论】:

    【解决方案2】:

    如果我使用 .h,请包括在内。如果我不使用 .h ..

    我猜你包括喜欢 -

    #include <vector.h>
    #include <ifstream.h>
    

    .h 已弃用,不应用于 C++ 标头。所以,改成 -

    #include <vector>
    #include <ifstream>
    

    它们都在 std 命名空间中定义。因此,您应该使用 using 指令导入它。

     using namespace std; // Probably missing this and is the cause for the errors
                          // vector and ifstream as undeclared identifiers.
    

    【讨论】:

    • @user - 有了你的声明 - " if i donot use .h, then it would show error in the body",我很确定编译器能够找到相应的头文件。因此,您使用的是 Visual Studio 中默认的 C++ 编译器。如果它是C 编译器,它肯定会在#include &lt;vector&gt; 语句中退出,但不会在函数体中退出。 ideone.com/9p1Pk
    猜你喜欢
    • 2014-07-18
    • 2018-03-23
    • 2011-03-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 2013-04-07
    相关资源
    最近更新 更多