0

I have a code (C++) using templates and it works. I have the same code in another project and I get LINK2019 error. What I did: I deleted all other files and just kept a few test .h and .cpp files and opened a new project to avoid any interference with other files or code. I still get LINK2019 error! I have a similar example in another project which is working and I think I am missing something here. If I change the template to a function/method then I don't get LINK2019 in the simplified project.

I put the error message and the code below. Please let me know if you could see what I am missing:

Error: Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "public: void __thiscall TestUnit::Method(class TestIO)" (??$Method@VTestIO@@@TestUnit@@QAEXVTestIO@@@Z) referenced in function _main Test of Link Issue I:\Me\My C++\Sizing Modules\Process Equipment Sizing\Test of Link Issue\Test of Link Issue\Main.obj 1

Code: Main.cpp

       #include "TestIO.h"
       #include "TestUnit.h"
       #include <iostream>
       using namespace std;

       int  main()
       {
       TestUnit TU100;

       TestIO TestIO100;
       TU100.Method(TestIO100);
       //TU100.Method<TestIO>(TestIO100);
       //TU100.function();//This works!

       system("pause");
       return 1;
       }

////////////////////////////////////////////////

TestUnit.h

     class TestUnit
     {
          public:
      TestUnit();
      virtual ~TestUnit();

      template <class T>
      void Method(T IO);

      void function(void);

     };

////////////////////////////////////////////////////////// TestIO.h

          class TestIO
          {
                public:
            TestIO();
            virtual ~TestIO();

            double pressure = 1000;

          };

//////////////////////////////////////////// TestIO.h

     #include "TestIO.h"
     TestIO::TestIO() {}
     TestIO::~TestIO() {}

////////////////////////////////////////////////////////// TestUnit.cpp

       #include "TestUnit.h"
       #include <iostream>
       using namespace std;

       TestUnit::TestUnit() {}
       TestUnit::~TestUnit() {}

       template <class T>
       void TestUnit::Method(T IO)
       {
         cout << "In the template: Salute to C++!!!" << endl;

        //cout << "Here in TestUnit Pressure: " << IO.pressure << endl;

       }

       void TestUnit::function(void)
       {
        cout << "We love C++ !!!" <<  endl;
       }
Eric A
  • 11
  • 4

1 Answers1

0

Template methods can't go into the .cpp file because the compiler doesn't know what template parameter you're going to use. Put it in the .h file instead so it's defined at the place you try to call it.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • I have already a similar program with the templates methods declared in h files and defined in cpp file and it is both compiled and linked and it's working. Also, I have a version of this working code which is all in cpp file and it's working. I can give the working code here if necessary. I think I am just missing a tiny thing here that causes this error. According to the definition of error 2019, there should be something which is not matching! – Eric A Jul 20 '17 at 04:16
  • I do have an example that templates declaration and definition is all in a cpp file and it works!!!!!! – Eric A Jul 20 '17 at 04:17
  • @EricA if both the definition and the call are in the same .cpp file, and the function definition comes before the call, then it will work fine. The question to ask yourself is, what does the compiler know at the time it reaches the function call? – Mark Ransom Jul 20 '17 at 04:42