C++ 矢量回推错误

c++ vector push back error

本文关键字:错误 C++      更新时间:2023-10-16
每次

我尝试使用 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:blablaDebugtest2.exe : fatal error LNK1120: 1 unresolved externals

我做错了什么?

看起来好像您正在构建项目的调试版本,但您正在链接到 C 运行时 DLL 的非调试版本。 您可以在以下位置签到:

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

运行时库应列为:"多线程调试 DLL (/MDd)",用于调试版本。

您实际上应该发现该项目构建为"发布"很好,因为CrtDbgReportW在发布版本中没有被std::vector调用,因此不需要在链接时找到该符号。