0

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?

Himilou
  • 127
  • 10
  • I don't see any definition of `testkeybase`. This `static wstring testkeybase;` is a declaration not a definition. You must also define it outside of the class. Not sure how you would do that, given all the macros you are using. Or you could just make it non-static. – john Dec 21 '20 at 09:05
  • You have mismatched `{}` in the code you have posted. You also have identical names for a namespace and a class. Makes it hard to say exactly what the correct code is. – john Dec 21 '20 at 09:17
  • Mismatched {} was a typo. As far as the namespace and class names that is what Visual Studio creates when you add a test project I just renamed them appropriately (default is namespace UnitTest1 and TEST_CLASS(UnitTest1)' – Himilou Dec 21 '20 at 22:26
  • The idea here was to have some public members that contained values to test with. I actually worked around this by creating a separate class and just declaring the members as publics inside that, then creating a class instance in each of the TEST_METHODS. Thanks john! – Himilou Dec 21 '20 at 22:33
  • Glad you got it working. It could have worked with a static data member as well if you'd added a definition for it. I just couldn't say exactly what that code needed to be because of the issues I mentioned. – john Dec 22 '20 at 06:13

0 Answers0