10

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

Amin Khormaei
  • 379
  • 3
  • 8
  • 23

2 Answers2

20

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.

iavr
  • 7,547
  • 1
  • 18
  • 53
4

You've got a static variable in your Math class. You need to provide a definition for it. To do this you can add:

int Math::result;

to your .cpp file

Sean
  • 60,939
  • 11
  • 97
  • 136