Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unresolved external symbol "private: static int Math::result" [duplicate]

Tags:

c++

static

this is my class definition:

#include <iostream>

using namespace std;

class Math
{
private:
    static int result;

public:
    static int add( int a , int b)
    {
        result = a + b ;
        return result;
    };
};

this is the main:

#include <iostream>
#include "Amin.cpp"

using namespace std;

int main()
{
    Math::add(2,3);
}

and i got these errors in visual studio:

error LNK2001: unresolved external symbol "private: static int Math::result" error LNK1120: 1 unresolved externals

best regards

like image 507
Amin Khormaei Avatar asked Feb 26 '14 20:02

Amin Khormaei


1 Answers

Just add

int Math::result;

in your cpp file.

Math::result is declared as a static data variable in the definition of Math and should be defined somewhere. This can be the cpp file containing main() or any other to be found by the linker. You need not and should not repeat the keyword static at the definition.

By the way, you should avoid using namespace std; (or any other namespace) in a header file.

like image 95
iavr Avatar answered Oct 11 '22 14:10

iavr