I'm trying to write unit tests for my classes but I'm getting "unresolved external symbol." There are no syntax errors and I have tried looking up the cause on the official Microsoft website but unfortunately it can be caused for a million different reasons. Any suggestions on what I can do or what might be causing it? This is my code:
// Example.h file
#pragma once
#include <iostream>
using namespace std;
class Example {
public:
int addTwoNumbers(int first, int second);
};
// Example.cpp file
#include "Example.h"
#include <iostream>
using namespace std;
int Example::addTwoNumbers(int first, int second) {
return first + second;
}
// Test.cpp file
#include "pch.h"
#include "../Project/Example.h"
#include "CppUnitTest.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace Tests
{
TEST_CLASS(Tests)
{
public:
TEST_METHOD(UnitTest1)
{
Example e;
Assert::AreEqual(4, e.addTwoNumbers(2, 2)); // this line is causing issues
}
};
}
The exact error that I'm getting:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: int __thiscall Example::addTwoNumbers(int,int)" (?addTwoNumbers@Example@@QAEHHH@Z) referenced in function "public: void __thiscall Tests::Tests::UnitTest1(void)" (?UnitTest1@Tests@1@QAEXXZ) Tests C:\Users\me\source\repos\Tests\Tests.obj 1