0

I've been struggling with this error on a more complex solution for days, so I simplified a test project to see if I can't figure it out and it still gets the same error:

1>------ Build started: Project: test, Configuration: Debug Win32 ------
1>  Test.cpp
1>  MainThing.cpp
1>  main.cpp
1>  Generating Code...
1>MainThing.obj : error LNK2019: unresolved external symbol "public: static int __cdecl Test::add(int)" (?add@Test@@SAHH@Z) referenced in function "public: void __thiscall MainThing::run(void)" (?run@MainThing@@QAEXXZ)
1>P:\Leif's Documents\Visual Studio 2013\Projects\test\Debug\test.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

MainThing.h

#pragma once
class MainThing
{
public:
    MainThing();
    ~MainThing();

    void run();

private:
    int result;
};

MainThing.cpp

#include "MainThing.h"
#include "Test.h"

#include <cstdlib>

#include <iostream>

MainThing::MainThing()
{
}


MainThing::~MainThing()
{
}

void MainThing::run() {
    int result = Test::add(5);
    std::cout << result;
}

Test.h

#pragma once
class Test
{
public:
    static int add(int a);
};

Test.cpp

#include "Test.h"

int add(int a){
    int b = a;
    int c = 5;
    int d = b + c;

    return d;
}

main.cpp

#include <iostream>
#include "MainThing.h"

int main(int argc, char** argv){
    MainThing mainGame;
    mainGame.run();

    return 0;
}

This has been super frustrating and the fact that it even effects a fairly simple recreation test without any external dependencies makes it that much more so.

LMP3D
  • 3
  • 2
  • You have declared a member function `Test::add`, but you haven't defined it. You have defined a standalone, non-member function `::add`, but you are never calling it. – Igor Tandetnik Dec 14 '15 at 04:44

1 Answers1

1

I think you just have a typo. Test.cpp should be

#include "Test.h"

int Test::add(int a){
    int b = a;
    int c = 5;
    int d = b + c;

    return d;
}

The way you have, you've written a free function that other things in Test.cpp could use, but no one else knows about. The compiler is looking for Test's method called add and isn't finding one.

John Neuhaus
  • 1,784
  • 20
  • 32
  • 1
    I just realized that before I checked here, and... all my problems in my more complex program are gone... – LMP3D Dec 14 '15 at 09:07