Trying to create a small test, I had met a problem
#include "stdafx.h"
#include "CppUnitTest.h"
#include "CppUnitTestAssert.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace SprintfTestNamespace
{
TEST_CLASS(SprintfTest)
{
public:
static char output[100];
static const char * expected;
TEST_METHOD_INITIALIZE(SprintfTestInit)
{
memset(output, 0xaa, sizeof output);
SprintfTest::expected = "";
}
static void expect(const char * s)
{
expected = s;
}
static void given(int charsWritten)
{
Assert::AreEqual((int)strlen(expected), charsWritten,
L"result string length ");
Assert::AreEqual(expected, output, false,
L"result string content ");
Assert::AreEqual((char)0xaa, output[strlen(expected) + 1],
L"meaning of the first outer char after string");
}
TEST_METHOD(NoFormatOperations)
{
expect("hey");
given(sprintf(output, "hey"));
}
TEST_METHOD(InsertString)
{
expect("Hello World\n");
given(sprintf(output, "Hello %s\n", "World"));
}
};
char SprintfTest::output[100]; // notice these two lines!
const char* SprintfTest::expected;
}
If I remove the two marked lines at the end, I am getting the errors LNK2001:
unresolved external symbol "public: static char * SprintfTestNamespace::SprintfTest::output" (?output@SprintfTest@SprintfTestNamespace@@2PADA)
If I have them in place, everything works OK, it builds and links and tests. But why have I to define the class variables out of the class if I use them only inside it?