2013-06-27 38 views
1

我沒有使用任何模板,它不是一個靜態類或函數,所以我完全不知道爲什麼它在定義時會拋出LNK2001錯誤。 以下是完整的錯誤:未解析的外部符號,即使它已被定義?

1>mapgenerator.obj : error LNK2019: unresolved external symbol "private: class std::vector<int,class std::allocator<int> > __thiscall MapGenerator::Decode(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]@Z) referenced in function "private: void __thiscall MapGenerator::GenerateTileLayer(class TiXmlNode *)" ([email protected]@@[email protected]@@Z) 

My MapGenerator class;

class MapGenerator 
{ 
public: 
    //Constructor and destructor 
    MapGenerator(std::string tmxfile) : doc(tmxfile.c_str()) { Load(); } 
    ~MapGenerator(); 

    //Loads in a new TMX file 
    void Load(); 

    //Returns a map 
    void GetMap(); 


    //Reads the TMX document 
    void Read(); 


private: 
    //The TMX document 
    TiXmlDocument doc; 



    //Generates a tile layer 
    void GenerateTileLayer(TiXmlNode* node); 

    //Generates a tileset 
    void GenerateTileset(TiXmlNode* node); 

    //Generates an Object Layer 
    void GenerateObjectLayer(TiXmlNode* node); 

    //Generates a Map Object(Goes in the object layer) 
    void GenerateObject(TiXmlNode* node); 

    //Decodes the data 
    std::vector<int> Decode(std::string data); 

    bool loadOkay; 


}; 

而且在在.cpp隨附定義,

std::vector<int> Decode(std::string data) 
{ 
    //Represents the layer data 
    std::vector<int> layerdata; 

    //Decodes the data 
    data = base64_decode(data); 

    //Shift bits 
    for(unsigned int i = 0; i < data.size(); i+=4) 
    { 
     const int gid = data[i] | 
       data[i + 1] << 8 | 
       data[i + 2] << 16 | 
       data[i + 3] << 24; 

     //Add the resulting integer to the layer data vector 
     layerdata.push_back(gid); 

    } 

    //Return the layer data vector 
    return layerdata; 
} 

我打電話這樣的功能,

std::string test(node->FirstChild("data")->Value()); 
data = Decode(test); 

我不知道爲什麼它是抱怨,當一切看起來合適。 在附註上,我試着讓函數採用const char * const而不是std :: string,因爲這是Value()返回的值,但仍然收到LNK2001錯誤。 任何想法?

+4

您忘了將'MapGenerator ::'添加到您的定義('Decode')中。 –

回答

5
std::vector<int> Decode(std::string data) 

應該有::範圍解析操作的類名。

std::vector<int> MapGenerator::Decode(std::string data) 
        //^^^^^ 

因爲它是MapGenerator類的成員函數。

+0

我知道我很愚蠢。謝謝! –

+1

@DanielMartin我們犯錯了,因爲我們不是人,而是超人。不用謝。 – taocp

4

你應該這樣做:

std::vector<int> MapGenerator::Decode(std::string data) 
{