I have a class like
template
class LinkedListItem
{
public:
LinkedListItem(T value);
LinkedListItem(const LinkedListItem& rhs);
T getValue(void);
LinkedListItem& getNext(void);
void setNext(LinkedListItem& next);
LinkedListItem& operator=(const LinkedListItem& 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 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
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?
Answer
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.
No comments:
Post a Comment