2012-09-27 83 views
2

林每次我试图用Visual C++ 2008C++矢量推回错误

#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 *)" ([email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected]@@Z) 
E:\blabla\Debug\test2.exe : fatal error LNK1120: 1 unresolved externals 

我究竟做错了什么?

+5

好像你在混合调试和发布设置。代码应该工作。 –

+1

在附注上,我建议使用矢量作为缓冲区,而不是char []。顺便说一句,代码为我编译,必须是您的项目设置。 – Borgleader

+0

你可以在VC++ 2010或2012上试试它,因为它适用于它们。 –

回答

4

看起来好像您正在构建项目的调试版本,但是您正在链接到C-Runtime DLL的非调试版本。

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

运行时库应列为:“多线程调试DLL(/ MDD)”的调试版本您可以检查。

实际上您应该发现该项目的构建方式与Release相同,因为CrtDbgReportW在发布版本中不会被std::vector调用,因此无需在链接时查找该符号。

+0

它确实工作正常与释放!谢谢! 你可以告诉我不同​​的og链接我有些东西与调试和发布之间的不同吗?因为我从来没有在发布之前运行过。 – PeterBechP

+1

@PeterBechP我没有链接,但主要的区别是调试二进制文件没有优化,这意味着它们运行得更慢,但更容易在调试器中运行。编译器对发布二进制文件执行一系列优化,这使得它们很难调试(例如,阻止您在某些地方设置断点或查看某些值) – Benj

+0

好的。这对我有意义。但是,我可以只为我的项目使用版本吗? :-) – PeterBechP