【问题标题】:c++ vector push back error [closed]C++向量推回错误[关闭]
【发布时间】:2012-09-27 19:12:28
【问题描述】:

每次我尝试使用 Visual C++ 2008 进行调试时都会出错

#include <iostream>
#include <vector>
#include <string>
#include <fstream>

using namespace std;

void load(const char* filename) {
    vector <string*> vec;
    ifstream in(filename);
    char buffer[256];
    while(!in.eof()) {
        in.getline(buffer, 256);
        vec.push_back(new std::string(buffer));
    }
}

int main(int argc, char* args[]) {
    cin.get();
    return 0;
}

得到这个错误

Compiling...
main.cpp
Linking...
main.obj : error LNK2019: unresolved external symbol __imp___CrtDbgReportW referenced in function "public: __thiscall std::_Vector_const_iterator,class std::allocator > *,class std::allocator,class std::allocator > *> >::_Vector_const_iterator,class std::allocator > *,class std::allocator,class std::allocator > *> >(class std::basic_string,class std::allocator > * *,class std::_Container_base_secure const *)" (??0?$_Vector_const_iterator@PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QAE@PAPAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@1@PBV_Container_base_secure@1@@Z)
E:\blabla\Debug\test2.exe : fatal error LNK1120: 1 unresolved externals

我做错了什么?

【问题讨论】:

  • 似乎您正在混合调试和发布设置。代码应该可以工作。
  • 附带说明,我建议使用vector 作为缓冲区,而不是char[]。顺便说一句,为我编译的代码,一定是你的项目设置。
  • 你能在 VC++ 2010 或 2012 上试试吗,因为它适用于它们。
  • 代码在我的电脑上运行良好,使用 VS2012 Express。
  • @ecbrodie 或者更好的是 git 摆脱 using namespace std; 并在其他任何地方添加 std::

标签: c++ vector fstream push-back


【解决方案1】:

看起来您正在构建项目的调试版本,但您链接的是 C-Runtime DLL 的非调试版本。你可以检查这个:

[Project] -> Properties -> C/C++ --> Code Generation --> Runtime Library

运行时库应列为:“多线程调试 DLL (/MDd)”,用于调试构建。

实际上,您应该会发现该项目作为“发布”构建得很好,因为 CrtDbgReportW 在发布构建中未被 std::vector 调用,因此不需要在链接时找到该符号。

【讨论】:

  • 它在发布时运行良好!谢谢!你能告诉我不同​​的 og 链接我一些与调试和发布之间不同的东西吗?因为我以前从来没有用 release 跑过。
  • @PeterBechP 我手头没有链接,但主要区别在于调试二进制文件没有经过优化,这意味着它们运行得更慢但更容易在调试器中运行。编译器对发布二进制文件执行一系列优化,使它们难以调试(例如,阻止您在某些地方设置断点或查看某些值)
  • 好的。这对我来说很有意义。但是我可以只为我的项目使用发布吗? :-)
  • @PeterBechP 我强烈建议您在完成开发后使用发布版本。但是,在您进行修复时,调试很方便,我经常在两者之间切换。
猜你喜欢
  • 2014-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-15
  • 2023-04-03
  • 1970-01-01
  • 2021-10-14
相关资源
最近更新 更多