1
votes

I have a class like

template <class T>
        class LinkedListItem
        {
        public:
            LinkedListItem(T value);
            LinkedListItem(const LinkedListItem<T>& rhs);
            T getValue(void);
            LinkedListItem<T>& getNext(void);
            void setNext(LinkedListItem<T>& next);
            LinkedListItem<T>& operator=(const LinkedListItem<T>& rhs);
            ~LinkedListItem();
        private:
            T _value;
            LinkedListItem& _next;
        };

I am trying to write a unit test like

TEST_CLASS(LinkedListUnitTests)
{
public:

    TEST_METHOD(Add_to_An_Empty_Linked_List)
    {
        LinkedListItem<int> item(1);
    }
//private:

};

When I try to just build the above code I get the ugly error -

error LNK2019: unresolved external symbol "public: __thiscall cpp::libraries::datastructures::LinkedListItem::LinkedListItem(int)" (??0?$LinkedListItem@H@datastructures@libraries@cpp@@QAE@H@Z) referenced in function "public: void __thiscall CppLibrariesTests::LinkedListUnitTests::Add_to_An_Empty_Linked_List(void)" (?Add_to_An_Empty_Linked_List@LinkedListUnitTests@CppLibrariesTests@@QAEXXZ)

I am using Visual Studio 2012.

Interestingly, If I add template in the unit test class like below the compile error goes away but the tests are not discovered and I can't run them.

template<class T>
TEST_CLASS(LinkedListUnitTests){..}

I am trying to pick up C++ after a long time so I won't be surprised if I am doing something very stupid. Any thoughts anyone?

1

1 Answers

1
votes

Templates must ideally be implemented inline. Second pass of compiler cannot re use the CPP file that has the implementation. Or, you need to #include the CPP file also.

Refer this article