C++ vector 推回错误

标签 c++ vector fstream push-back

每次尝试使用 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

我做错了什么?

最佳答案

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

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

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

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

关于C++ vector 推回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12628507/

相关文章:

c++ - 使用 cin 跳过预期的字符,如 scanf()

c++ - 出现奇怪的错误.. Microsoft C++ 异常:内存位置 0x002cf6 处的 std::out_of_range

c++ - 将 vector<fooType> foo[num] 移植到 Windows(无 C99)

c++ - 如何在c++中使用fstream将数据写入大于2GB的文件

c++ - 如何从 Alexandrescu A. 'Append' 编译模板类 "Modern C++ Design. Generic Programming and Design Patterns Applied"?

c++ - OpenCV:HOGDescriptor.compute

c++ - 从 Delphi 调用 c++ 类库

matlab - 在Matlab中根据从1到n的和创建一个函数

c++ - 从文件中读取空格分隔的数字直到换行符

C++: .eof 在一个空文件上