2012-09-27 86 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