This problem seems a little strange to me, but the linker seems to be unable to find the class member variables of the class it is compiling. Note THIS IS NOT that the test class cannot find symbols for the class it is testing, but rather it cannot find the symbols that belong to itself.
following two files are part of the registryreader project and the ReadValue function is what we want to test. test target header
//RegReader.h
#include <windows.h>
#include <string>
class RegReader
{
public:
wstring ReadValue();
}
test target cpp file
//RegReader.cpp
#include RegReader.h
wstring RegReader::ReadValue()
{
wstring returnvalue = blahblahCallToReadRegValue();
return returnvalue;
}
following is in the registryreaderunittest project registryreaderunittest.cpp
//registryreaderunittest.cpp
#include "pch.h"
#include <vector>
#include <string>
#include "..\lib\ApiRegReader.h"
#include "CppUnitTest.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace std;
namespace ApiRegReaderUnitTest
{
TEST_CLASS(ApiRegReaderUnitTest)
{
public:
static wstring testkeybase;
TEST_CLASS_INITIALIZE(ApiRegReaderTestSetup)
{
Init();
}
TEST_CLASS_CLEANUP(ApiRegReaderTestCleanup)
{
DeleteTestRegKey();
}
TEST_METHOD(CreateDword)
{
RegReader r;
wstring value = r.ReadValue();
Assert::AreEqual(testkeybase, value);
}
static void Init();
};
void ApiRegReaderUnitTest:Init()
{
testkeybase = L"valuetoread";
}
}
Now the reg reader project compiles with no issues. When I try and compile the unit test, either as part of the solution or by iteself I get
error LNK2001: unresolved external symbol "public: static class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > ApiRegReaderUnitTest::ApiRegReaderUnitTest::testkeybase" (?testkeybase@ApiRegReaderUnitTest@1@2V?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@A)
why can the linker not find the wstring
that is located in its own obj file?