2014-05-02 30 views
1

好的,所以我有一個函數,它接受一個字符串並以特定方式將其轉換爲矢量。該代碼是:更改矢量時發生幾乎不可讀的錯誤

std::vector<std::string> delimit_string(std::string s, std::string delimiter) { 
    std::vector<std::string> outVector; 
    size_t pos = 0; 
    std::string token; 
    while ((pos = s.find(delimiter)) != std::string::npos) { //thx http://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c 
     token = s.substr(0, pos); 
     outVector.push_back(token); 
     s.erase(0, pos + delimiter.length()); 
    } 
    return outVector; 
} 

std::vector<std::string> parse_map(std::string map, SDL_Renderer* ren) { 
    std::vector<std::string> mapString = delimit_string(map, "^"); 
    for (int i = 0; i < mapString.size(); ++i) { 
     size_t pos; 
     while ((pos = mapString[i].find("|")) != std::string::npos){ //search for a string "|" or exit if it isnt found 
      mapString[i].erase(pos, pos); 
      std::string string1 = mapString[i].substr(0, pos); //from pos to beginning 
      std::string string2 = mapString[i].substr(pos + 1, std::string::npos); //from pos+1 to end 
      mapString.erase(mapString.begin() + i); 
      mapString.insert(mapString.begin() + i, string2); 
      mapString.insert(mapString.begin() + i, "|"); 
      mapString.insert(mapString.begin() + i, string1); 
     } 
    } 
    //now mapstring looks somewhat like this: 
    //[commoner.bmp, commoner.bmp, |, commoner.bmp, commoner.bmp] 
    return mapString; 
} 

如果我通過它像"hello.bmp^hello.bmp|hello.bmp"的東西,它應該返回,看起來有點像["hello.bmp", "hello.bmp", "|", "hello.bmp"]

雖然是矢量,而不是返回這一點,在編譯時使用此功能錯誤(而不可讀)錯誤:

Error 1 error LNK2019: unresolved external symbol "class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > __cdecl parse_map(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" ([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) referenced in function _SDL_main C:\Users\spng453\documents\visual studio 2013\Projects\loot\loot\main.obj loot 

我知道,通過排除法,即parse_map函數是在編譯時導致錯誤的功能,它很可能是由於我不千牛由於媒介如何運作。任何幫助將

+0

大概在函數原型和功能不匹配的執行參數列表;仔細檢查它們。 –

+0

非常感謝。函數原型只是'std :: string map',不包含渲染器。這是我做過的最愚蠢的錯誤。 – Aearnus

回答

1

這通常發生在你的函數聲明(原型)和定義(實現)不匹配;編譯器假定原型中聲明的函數是在其他地方定義的,所以錯誤僅在鏈接時被捕獲。

你可以很容易地看到這從您的錯誤信息:該接頭用在尋找這樣一個功能:

std::vector<std::string > parse_map(std::string) 

(你 std::string更換 std::basic_string< ... lots of stuff ...>,這是它的一個類型定義後獲得此)

,而你的實現是:

std::vector<std::string> parse_map(std::string map, SDL_Renderer* ren) 
2

看仔細瞭解一下你得到的錯誤:

unresolved external symbol "class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > __cdecl parse_map(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)"

簡化,即

unresolved external symbol "class std::vector<class std::string> __cdecl parse_map(class std::string)"

注意,還有就是SDL_Renderer *參數沒有提及。這意味着你可能會在其他地方聲明這個函數(也許在頭文件中?),聲明它沒有第二個參數SDL_Rendered *。確保所有聲明符合定義並重建所有代碼。

+0

這就是問題所在。謝謝你用1分鐘的答案StackOverflow! – Aearnus