【问题标题】:How to do in-file/out-file in VS2013, C++如何在 VS2013,C++ 中做 in-file/out-file
【发布时间】:2015-06-26 11:00:32
【问题描述】:

我是 C++ 编程新手,最初在 VS 2010 中学习了基础知识。我升级到 VS2013,并尝试声明我的 ifstream/ofstream 变量。在我删除或注释掉它们之前,它将无法构建我的程序。 下面的代码只是我定义一个类的头文件,我打算从父类(字符)继承和保存变量。以下所有内容都在我的 CPP 中。

感谢那些已经回答的人,我删除了 istream,并且我的命名空间存在,但我没有提到这一点,我为我的不明确表示歉意。 但是,该程序仍然会产生前面提到的错误。

    #include <iostream>
    #include <string>
    #include <ctime>
    #include <limits>
    #include <Windows.h>
    #include <cmath>
    #include <fstream>
    #include <iomanip>

    using namespace std;

    class saveLoad : public character 
    { 
        public:     
        saveLoad();     
        void saveGameFunc();    
        void loadGameFunc();

        ifstream loadGame;  
        ofstream saveGame;
     };

我收到的错误是:

错误 39 错误 LNK2019:无法解析的外部符号 __imp___CrtDbgReportW 在函数“public: char const & __thiscall std::_String_const_iterator > >::operator*(void)const”中引用 (??D?$_String_const_iterator@V?$_String_val@U?$_Simple_types@D@std@@@std@@@std@@QBEABDXZ) E:\Text Rpg\Text Rpg\Game.obj 文字游戏

Error 40 error LNK1120: 1 unresolved externals E:\Text Rpg\Debug\Text Rpg.exe 1 1 文本 Rpg

有人可以向我解释在 VS2013 中声明/定义 in-file 和 out-file 的正确方法吗?

【问题讨论】:

  • 当前形式的代码甚至不是有效的 C++。
  • 在我添加了一个空字符类、为 saveload 类中列出的 3 个函数提供空实现并添加了一个主函数之后,您的代码在 Visual Studio 2013 中编译得很好。这意味着错误出现在您没有向我们展示的代码中。请构建一个简短完整的问题示例并将其编辑到问题中。
  • 您也可以在这里阅读答案:stackoverflow.com/questions/11919836/unresolved-external 您的项目可能设置不正确。
  • 我的项目设置不正确。查看您推荐的线程后,我的程序现在可以编译。感谢您的帮助,非常感谢!

标签: c++ visual-studio-2013 fstream istream


【解决方案1】:

你没有声明命名空间 尝试改变

ifstream loadGame;
ofstream saveGame;

std::ifstream loadGame;
std::ofstream saveGame;

【讨论】:

    【解决方案2】:

    不应该是std::ifstreamstd::ofstream 吗?

    除非您在该文件中使用了std::ifstreamstd::ofstream,但没有发布。 std::ifstreamstd::ofstream 都在 &lt;fstream&gt; 标头中,因此您实际上不需要 &lt;istream&gt; 一个,除非您将它用于其他用途。

    这里还有一个编译器错误:

    ofstream saveGame:
    

    应该是:

    ofstream saveGame;
    

    【讨论】:

      猜你喜欢
      • 2023-02-24
      • 1970-01-01
      • 2022-12-28
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      • 2023-02-02
      相关资源
      最近更新 更多