使用STL容器时出现链接错误

时间:2013-05-05 06:46:20

标签: c++ visual-studio-2008 stl

我在VS2008中运行我的C ++代码时遇到了链接错误,我使用了C ++ STL:

error LNK2001: unresolved external symbol "public: static class std::_Tree<class 
std::::_Tmap_traits<class std::basic_string<char,struct std::char_traits<char>,
class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>
,class std::allocator<char> >,struct std::less<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct
std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::
allocator<char> > const ,class std::basic_string<char,struct std::char_traits<char>,
class std::allocator<char> > > >,0> >::iterator parser::parsedDataIterator"
(?parsedDataIterator@parser@@2Viterator@?$_Tree@V?$_Tmap_traits@V?$basic_string@DU?$
char_traits@D@std@@V?$allocator@D@2@@std@@V12@U?$less@V?$basic_string@DU?$char_traits
@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char
_traits@D@std@@V?$allocator@D@2@@std@@V12@@std@@@2@$0A@@std@@@std@@A)   parser.obj

我没有将任何库与代码相关联,不知道是否有相同的需要。

以下是成功编译的代码:

//Including header file that just contains the class declaration
#include "parser.h"

//Static variable definitions...
int parser::lang = -1;
std::string parser::fullString;
std::map<std::string, std::string> parser::parsedData;
std::map<std::string, std::string>::iterator parsedDataIterator;
std::stack<char> parser::symbolStack;

//The parsing function used to parse XML content..
bool parser::XMLParser(std::string arg_String)
{   
    //Create empty strings...
    std::string tagText;
    std::string emptyString = "";
    //Maintain count for string length
    int count = 0;

    //Loop over whole string...
    for(unsigned int i=0 ; i<arg_String.length() ; i++)
    {
        //ignore white string when not reading internal tag text content...
        cout<<"Value of i : "<<i<<" ... Value of str.char is :"<<arg_String[i]<<endl;
        if(arg_String[i] == ' ' && count==0)
            continue;

        if(arg_String[i] == '<'){
            symbolStack.push(arg_String[i]);
            tagText.clear();
        }
        else if(arg_String[i] == '>'){
            symbolStack.pop();
            parsedData[tagText] = "";
        }
        else{
            tagText.push_back(arg_String[i]);
            count++;
        }


    }
    if(symbolStack.empty())
    {
        cout<<"XML parsing was successful :"<<endl;
        return true;
    }
}

bool parser::populateAndReturn(std::string arg_String)
{
    bool status = false;
    status = XMLParser(arg_String);
    return status;

}

int main(int argc, char *argv[])
{
    std::string inputString = "<html>";
    if(parser::populateAndReturn(inputString))
    {
        for(parser::parsedDataIterator = parser::parsedData.begin(); parser::parsedDataIterator != parser::parsedData.end(); ++parser::parsedDataIterator)
        cout << "Key: " << (*parser::parsedDataIterator).first << " Value: " << (*parser::parsedDataIterator).second;
    }
    else
        cout<<"\nError encountered while parsing";
    system("pause");
}

“parser.h”的代码:

#pragma once

#include<iostream>
#include<fstream>
#include <map>
#include <stack>
#include <string>

using namespace std;

class parser
{
    static std::string fullString;
        //Declaring a map that will associate the tag with the container text.
    static std::stack<char> symbolStack;

public: 
    static std::map<std::string, std::string> parsedData;
    static std::map<std::string, std::string>::iterator parsedDataIterator;

    parser(){
    }

    static int lang;

    static enum format{
        XML,
        JSON,
        ZZZ = -1
    };

    static bool populateAndReturn(std::string arg_String);
    static bool XMLParser(std::string arg_String);
};

1 个答案:

答案 0 :(得分:1)

您从解析器:: parsedDataIterator中取消了类名。

static成员的定义中,更改:

std::map<std::string, std::string>::iterator parsedDataIterator;

为:

std::map<std::string, std::string>::iterator parser::parsedDataIterator;
                                           //^^^^^^^^