0

The last version of C++ IDE I was using is VC98, so a lot of things have changed and I'm confused.

I'm trying to create a class MyParser. I have its header file MyParser.h in Header Files in the solution explorer, and the source file MyParser.cpp in Source Files.

MyParser.h looks like below.

#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

using namespace std;

class MyParser
{
private:
    boost::property_tree::ptree pt;

public:
    MyParser(string str);
};

MyParser.cpp is:

#include "MyParser.h"
#include <iostream>

using namespace std;

MyParser::MyParser(std::string str)
{
    boost::property_tree::read_xml(str, this->pt);
}

The main.cpp (in a separate project under the same solution) is

#include <string>
#include <iostream>
#include "MyParser.h"

using namespace std;

int main()
{
    string content = "some xml string.....";

    MyParser parser = MyParser(content);

    return 0;
}

Then I've got LNK2019 error:

unresolved external symbol "public: __cdecl MyParser::MyParser(class std::basic_string,class std::allocator >)" (??0MyParser@@QEAA@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function main test C:\MySpace\test\test.obj

However, as soon as I move the implementation of the constructor into MyParser.h, the error disappears.

Could anyone point it out to me what is wrong with my code/config? Many thanks

ChangeMyName
  • 7,018
  • 14
  • 56
  • 93
  • 2
    I suspect that `MyParser.cpp` is not part of your project. A quick way to test is to make an intentional error in that file to see if the compiler complains. If it does not then you are not building it.. – drescherjm Sep 16 '19 at 13:05
  • Not related to your problem, but don't do `using namespace std` – kuro Sep 16 '19 at 13:07
  • @kuro Why is that? – ChangeMyName Sep 16 '19 at 13:08
  • 3
    [https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – drescherjm Sep 16 '19 at 13:09
  • @ChangeMyName could you append a picture of your solution explorer? – nikoksr Sep 16 '19 at 13:11
  • I second @drescherjm. I tried to make a VS2019 project with the three files shown above, and it compiled no problem. Then I deleted the `MyParser.cpp` file from the project and got the exact same LNK2019: unresolved external symbol "public: __thiscall MyParser::MyParser( ... ) referenced in function _main error as OP does. – Frodyne Sep 16 '19 at 13:18
  • 1
    @Frodyne Thanks mate. Then what change should I make in the configs? – ChangeMyName Sep 16 '19 at 13:22
  • 2
    Well, I got that error by deleting the .cpp file from my project, so I guess the fix is to add it again... Try to double-check if you have the file added to your project. – Frodyne Sep 16 '19 at 13:28

0 Answers0