There is a class Point, defined in the header file Header.h, all methods, constructors, destructors are described there.

When you try to build a project all the time, it knocks out the error LNK2001 and LNK1120, please tell me what could be the problem.

Header.h:

#include <iostream> using namespace std; class Point { private: static int _freeID; const int _pointID; double _x; double _y; public: Point(const double &x = 0, const double &y = 0): _x(x), _y(y), _pointID(++_freeID){ } Point(const Point& u) : _x(ux()), _y(uy()), _pointID(++_freeID) {} ~Point(void) { return; } Point& operator=(const Point &u) { _x = ux(); _y = uy(); return *this; } double& x(void) { return _x; } double& y(void) { return _y; } const double& x()const { return _x; } const double& y()const { return _y; } }; ostream& operator<<(ostream& os, const Point& u) { os << '(' << ux() << ', ' << uy() << ')'; return os; } const Point operator+(const Point& a, const Point& b) { return Point(ax() + bx(), ay() + by()); } Point& operator+=(Point&, const Point&); const bool operator==(const Point& a, const Point& b) { if (ax() == bx() && ay() == by()) return true; else return false; } const bool operator!=(const Point& a, const Point& b) { if (ax() == bx() && ay() == by()) return false; else return true; } 

Main.cpp:

 #include "stdafx.h" #include "Header.h" int main() { Point a(1, 2); cout << a; system("pause"); return 0; } 

    2 answers 2

    I guess the problem is as follows. You have a static class field ( static int _freeID ) that is not defined anywhere (just declared). You must explicitly define it outside the class. Add a string after class definition

     int Point::_freeID; 

    In this case, the field will be initialized with the default value, i.e. 0. You can explicitly specify the initializing value of the variable.

     int Point::_freeID = 1; 

    Linking problem should disappear.

    • Thanks, helped - Oleksandr Zakrevskyi

    Declare your class in header.h, and define the code in header.cpp

    • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky