0

The project has 2 classes - Tree and TreeTest class

The TreeTest class tests the functions of Tree class.

  • TreeTest.h

    #ifndef TREETEST_H
    #define TREETEST_H
    
    class TreeTest
    {
       public:
         TreeTest();
         virtual ~TreeTest();
         void InitTreeFunctionTest();
       protected:
       private:
    };
    #endif // TREETEST_H
    
  • TreeTest.cpp

    #include "TreeTest.h"
    #include "Tree.h"
    #include <cstring>
    #include <iostream>
    
    using namespace std;
    
    void TreeTest::InitTreeFunctionTest()
    {
       //code goes here
    }
    
  • Main.cpp

    #include <iostream>
    #include <Tree.h>
    #include <TreeTest.h>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        TreeTest* tt;
        tt->InitTreeFunctionTest();
    }
    

But it gives the following error when I compile using

g++ -fprofile-arcs -ftest-coverage main.cpp -I<full path to library> -o test

undefined reference to `TreeTest::InitTreeFunctionTest()'

Can anyone please help me find the error?

Thanks

SGh
  • 41
  • 8

2 Answers2

0

You have not compiled TreeTest.cpp

Try adding it to your command line:

g++ -fprofile-arcs -ftest-coverage TreeTest.cpp main.cpp -I<full path to headers> -o test

edit:

You will also need to provide the definitions of the constructor and destructor in TestTree.cpp

The other solution is to inline the function definition in the header. But, you probably want to compile it unless the functions are trivial.

bendervader
  • 2,619
  • 2
  • 19
  • 22
0

Please use #include "TreeTest.h", not #include <TreeTest.h>. If you want to use #include <TreeTest.h>, first, you must make your TreeTest.cpp into a static library. BTW, you can search for the difference between #include "" and #include <>. I hope this can help you.

cwfighter
  • 502
  • 1
  • 5
  • 20