質問

Okay, so I have a function that takes a string and converts it into a vector in a specific way. The code is:

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;
}

If I were to pass it something like "hello.bmp^hello.bmp|hello.bmp" it should return a vector that looks somewhat like ["hello.bmp", "hello.bmp", "|", "hello.bmp"]

Though, instead of returning this, the function errors at compile time with this (rather unreadable) error:

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> >)" (?parse_map@@YA?AV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@2@@Z) referenced in function _SDL_main  C:\Users\spng453\documents\visual studio 2013\Projects\loot\loot\main.obj   loot

I know, through process of elimination, that the parse_map function is the function that is causing errors at compile time, and it is probably due to me not knowing how vectors work at all. Any help would be appreciated

役に立ちましたか?

解決 2

This typically happens when your function declaration (the prototype) and the definition (implementation) don't match; the compiler assumes that the function declared in the prototype is defined elsewhere, so the error is caught only at link-time.

You can easily see this from your error message: the linker is looking for a function like this:

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

(you obtain this after replacing std::basic_string< ... lots of stuff ...> with std::string, which is a typedef for it)

while your implementation is:

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

他のヒント

Look carefully at the error you got:

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> >)"

Simplified, that is

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

Notice that there is no mention of the SDL_Renderer * parameter. What this means is that you may have a declaration of this function somewhere else (maybe in a header file?) that declares it without the second SDL_Rendered * parameter. Ensure that all your declarations match the definition and rebuild all your code.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top